既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
pytest实际上是python自带测试框架unittest的扩展,那么pytest是如何实现unittest中的setup和teardown的呢?
pytest初始化的类别和作用域
- 模块级别(Module level setup/teardown):作用于一个模块内的所有class和def,对于所有class和def,setup和teardown只执行一次
def setup\_module(module):
"""
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减少输出信息:
test_method结果.png
*
2、setup_class(cls)
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
4107)]
[外链图片转存中…(img-yCnOizcn-1715498044107)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新