既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
setup any state specific to the
execution of the given module.
“”"
def teardown_module(module):
“”"
teardown any state that was previously
setup with a setup_module
method.
“”"
* 类级别(Class level setup/teardown):作用于一个class内中的所有test,所有用例只执行一次setup,当所有用例执行完成后,才会执行teardown
@classmethod
def setup_class(cls):
“”" setup any state specific to the execution
of the given class (which
usually contains tests).
“”"
@classmethod
def teardown_class(cls):
“”" teardown any state that was previously
setup with a call to
setup_class.
“”"
* 方法和函数级别(Method and function level setup/teardown):作用于单个测试用例,若用例没有执行(如被skip了)或失败了,则不会执行teardown
def setup_method(self, method):
“”"
setup any state tied to the execution
of the given method in a
class. setup_method is invoked for
every test method of a class.
“”"
def teardown_method(self, method):
“”"
teardown any state that was
previously setup with a setup_method
call.
“”"
若用例直接写在模块中,而不是在类中,则用:
def setup_function(function):
“”"
setup any state tied to the execution of the given function.
Invoked for every test function in the module.
“”"
def teardown_function(function):
“”"
teardown any state that was previously setup with
a setup_function call.
“”"
* pytest.fixture()装饰函数,结合yield实现初始化和teardown
举个例子(pytest)文档中的:
import smtplib
import pytest
@pytest.fixture(scope=“module”)def smtp():
smtp = smtplib.SMTP(“smtp.gmail.com”, 587, timeout=5)
yield smtp # provide the fixture value
print(“teardown smtp”)
smtp.close()
运行结果:
$ pytest -s -q --tb=no
FFteardown smtp2 failed in 0.12 seconds
#### pytest用例初始化操作的示例
为了体现初始化和环境恢复,本节演示采用邮件发送的脚本,可查看邮件发送的脚本:python发送邮件脚本或者参考文章:SMTP发送邮件。
##### 1、setup\_method(self, method)
* 在test\_method.py中构建了3个测试用例,每个用例在执行前后都会执行setup\_method/teardown\_method连接smtp和断开smtp。
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import pytest
class TestSmtp():
# 发件人和收件人,换成你自己的发件人、收件人qq号
sender = “sender@qq.com”
receivers = “rece@qq.com”
# 邮箱服务器
smtpserver = "smtp.qq.com"
smtpport = 465
# 连接邮箱服务器,qq邮箱和密码,换成自己的
username = "sendr@qq.com"
password = "qq mail's password"
smtp = smtplib.SMTP_SSL()
def setup\_method(self, method):
self.smtp.connect(self.smtpserver, self.smtpport)
self.smtp.login(self.username, self.password)
print("成功登录")
def teardown\_method(self, method):
self.smtp.quit()
print("断开连接")
def test\_send\_text(self):
# 邮件发送、接收人员,邮件标题、正文
msg = MIMEText("微信公众号号:开源优测", "plain", "utf-8")
msg["From"] = self.sender
msg["To"] = self.receivers
msg["Subject"] = Header("开源优测\_DeepTest\_from\_chenlele\_text", "utf-8")
# 发送邮件
self.smtp.sendmail(self.sender, self.receivers, msg.as_string())
def test\_send\_html(self):
msg = MIMEText("<p>微信公众号号:开源优测</p><a href='http://www.testingunion.com'>开源优测社区</a>>",
"html",
"utf-8")
msg["From"] = self.sender
msg["To"] = self.receivers
msg["Subject"] = Header("开源优测\_DeepTest\_from\_chenlele\_html", "utf-8")
# 发送邮件
self.smtp.sendmail(self.sender, self.receivers, msg.as_string())
def test\_send\_attchment(self):
# 邮件格式说明、发送、接收人员信息、邮件标题
msg = MIMEMultipart()
msg["From"] = self.sender
msg["To"] = self.receivers
msg["Subject"] = Header("开源优测\_DeepTest\_from\_chenlele", "utf-8")
# 构建带附件的邮件正文
msg.attach(MIMEText("微信公众号:开源优测", "plain", "utf-8"))
# 构造附件,多个附件同理
attach1 = MIMEText(open("judge\_leap.json", 'rb').read(), "base64", "utf-8")
attach1["Content-Type"] = "application/octet-stream"
# 这里filename随意写,将会在邮件中显示
attach1["Content-Disposition"] = "attrachment;filename=code.py"
# 关联附件到正文
msg.attach(attach1)
# 发送邮件
self.smtp.sendmail(self.sender, self.receivers, msg.as_string())
* 查看结果,采用pytest -s -q 运行,-s 可以查看打印信息,-q减少输出信息:
![](https://img-blog.csdn.net/20180412202810636?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podXNvbmd6aXll/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
test\_method结果.png
*
##### 2、setup\_class(cls)
* 作用于class的setup\_class/teardown\_class,类中所有的用例只会执行一次,如图所示;
* ps:用例与test\_method.py的一致,参考上一串代码。
![](https://img-blog.csdn.net/20180412202849187?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podXNvbmd6aXll/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
test\_class.png
##### 3、setup\_module(module)
* setup\_module/teardown\_module在一个模块内,只会执行一次,作用于模块内的所有用例
* 示例中构建了2个class和1个def,共4个用例,可以看到,4个用例只执行了一次module
![](https://img-blog.csdn.net/20180412202905187?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podXNvbmd6aXll/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![img](https://img-blog.csdnimg.cn/img_convert/4bea8c73c614346408afd118b29c8d5c.png)
![img](https://img-blog.csdnimg.cn/img_convert/74793259b7b2b984c35c9f5c410bf683.png)
![img](https://img-blog.csdnimg.cn/img_convert/352b6c18bd99648f5f69744933d8b1bb.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**
7416)]
[外链图片转存中...(img-wpYhxmGe-1715561077417)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**