vps服务器怎么创建多个网站,sem seo什么意思,标书制作教程视频网站,wordpress4.94中文版哈咯#xff0c;大家#xff0c;我们今天来学习pytest单元测试框架#xff0c;这个框架要比unittest更加易于使用#xff0c;结构性也更加好#xff0c;希望大家能够从中学习到有用的东西#xff0c;然后在下一章#xff0c;我们来使用pytest单元测试框架来搭建一个web自…哈咯大家我们今天来学习pytest单元测试框架这个框架要比unittest更加易于使用结构性也更加好希望大家能够从中学习到有用的东西然后在下一章我们来使用pytest单元测试框架来搭建一个web自动化测试项目一起加油直接上整体学习项目结构图pytest项目结构pytest1.安装pytestpip3 install pytest2.pytest框架下的测试文件和测试函数都要以test开头3.在命令行输入pytest即会执行test开头的文件4.pytest.main()也同样可以开始执行 - pytest会在当前命令行所在目录下全局搜索test开头的文件进行执行测试这里我们主要针对unittest 和 pytest的不同之处进行学习一、断言pytest并没有提供专门的断言方法而是直接使用assert进行断言如assert a bassert a in bassert abc() is Trueassert not abc()assert abc() is False二、Fixture是对测试方法、测试函数、测试类和整个测试文件进行初始化或还原测试环境例# 装饰器从session - module - class - function(都是针对每个里面包含的用例去执行,覆盖单位是用例)session:多个文件调用一次可以跨.py文件调用每个.py是一个modulemodule:每个.py文件调用一次每个.py是一个moduleclass:每个类调用一次function:每个函数或方法调用一次# 如果你想一个module下的都用上那就打开改成True, 如下这样就不需要往每个函数里传入fixture# yield xxx接着后续的代码实现tearDown的功能# 如果是把fixture单独存放定义一个conftest.py, 该文件要和case在同级目录下case文件中声明# content of conftest.pyconftest.py:# coding utf8import osos.path.abspath(.)from time import sleepimport pytesta py file which saved pytests fixture for usepytest.fixture(scope function)def before_case_execute():sleep(3)yield before_case_executesleep(3)test_sample.py:# coding utf8# content of conftest.pyimport osos.path.abspath(.)import pytestfrom time import sleep# 装饰器从session - module - class - function# 如果你想一个module下的都用上那就打开改成True, 如下这样就不需要往每个函数里传入fixture# yield实现tearDown的功能pytest.fixture(scope function)def before_each_case():sleep(3)print(Set up now)yield before_each_casesleep(3)print(Tear down now)def add(x):return x 1# def test_add(before_each_case):def test_add(before_case_execute):assert add(3) 4if __name__ __main__:pytest.main()三、参数化pytest的参数化与unittest类似通过pytest.mark.parameterize进行装饰传入需要的值名、值、以及为每一个值指定对应的case名test_parameterize.py:# coding utf8import osos.path.abspath(.)# pytest参数化import pytestpytest.mark.parametrize(x, y, z,[(1, 2, 9), (4, 5, 9), (7, 8, 5)],ids [case1, case2, case3])def test_add(x, y, z):assert x y z# terminal进入当前py文件路径,pytest -v test_parameterize.py指定执行# if __name__ __main__:# pytest.main([-v, -k add, ./test_parameterize.py])四、运行测试1.指定名称中包含某字符串的用例执行pytest -k add test_parameterize.py2.减少运行的冗长日志信息pytest -q test_parameterize.py3.如果出现一条测试用例失败则退出测试pytest -x test_parameterize.py4.运行测试目录pytest ./pytest_object5.指定特定类或方法执行pytest test_parameterize.py::test_add 用::进行分隔6.通过main()运行测试pytest.main([-v, -k add, ./test_parameterize.py]) 每个参数用逗号隔开五、生成测试报告1.生成JUnit XML文件pytest ./pytest_project --junit-xml./pytest_project/report/pytest_log.xml2.生成在线测试报告pytest test_parameterize.py --pastebinall 执行到最后会生成一个链接如下View paste B3CAbpaste.net六、conftest配置文件(conftest.py 是pytest特有到本地测试配置文件1.可以用来设置项目级别的Fixture2.导入外部插件3.指定钩子函数)pytest扩展1.生成HTML格式的测试报告pip3 install pytest-html pytest ./pytest_project --html./pytest_project/report/pytest_html_result.html2.在用例失败时重试pip3 install pytest-rerunfailurespytest -v pytest_expand.py --reruns 3(重试机制可以增加测试用例的稳定性避免因为一些网络原因、机器卡顿导致的问题)3.pytest-parallel扩展pip3 install pytest-parallelpytest -q pytest_expand.py --tests-per-worker auto(使用并行运行测试case测试可能会导致互相产生干扰需要谨慎使用)# coding utf8import osos.path.abspath(.)import pytestpytest扩展# pytest ./pytest_project --html./pytest_project/report/pytest_html_result.htmldef test_add():assert 1 1 3关注 收藏 点赞哦谢谢啦