男女做暖暖到网站庆元建设局网站
- 作者: 五速梦信息网
- 时间: 2026年03月21日 10:18
当前位置: 首页 > news >正文
男女做暖暖到网站,庆元建设局网站,烟台南山集团网站建设,科技无国界单测框架的作用
测试发现#xff1a;从多个文件中寻找测试用例。测试执行#xff1a;按照一定顺序去执行并且生成结果。测试断言#xff1a;判断最终结果与实际结果的差异。测试报告#xff1a;统计测试进度、耗时、通过率#xff0c;生成测试报告。 pytest简介
pytest是…单测框架的作用
测试发现从多个文件中寻找测试用例。测试执行按照一定顺序去执行并且生成结果。测试断言判断最终结果与实际结果的差异。测试报告统计测试进度、耗时、通过率生成测试报告。 pytest简介
pytest是python的单测框架使用灵活插件丰富以下是pytest常用的插件
pytestpytest-html生成html测试报告插件pytest-xdist多线程执行用例插件pytest-ordering自定义用例顺序插件pytest-rerunfailures失败重跑插件allure-pytest生成allure美观测试报告插件
pip install 就行只有有这些插件下面的某些命令行才生效 pytest默认规则
模块名必须以test_开头或者_test结尾类名必须以Test开头测试方法必须以test开头pytest用例运行顺序默认从上到下代码中可以使用装饰器pytest.mark.run(order1)来指定执行顺序
使用pytest.ini文件可以修改默认规则 pytest的运行方式 主函数模式 1 2 3 4 import pytest if name main: pytest.main([-vs, ./test_demo/test_demo1.py])
这样就可以运行所有用例 命令行模式 1 pytest -vs ./test_demo/test_demo1.py 参数详解
-s输出调试的信息-v表示详细的方式输出./test_demo/test_demo1.py表示运行指定模块相对路径表示./test_demo/test_demo1.py::TestCase1::test_case1 nodeid表示代表运行./test_demo/test_demo1.py模块下的TestCase1类的test_case1 方法-n分布式运行测试用例-n num参数num代表几个线程运行用例–reruns2表示用例失败重跑2次常用于一些不稳定的用例如web自动化-x只要有一个用例报错那么就会停止–maxfail2有2个用例失败就会停止-k根据测试用例部分字符串指定测试用例如 -k “ao”代表会执行带有ao名称的字符串 读取pytest.ini配置文件运行
不论是主函数模式还是命令行模式都会读取这个配置文件该文件需要使用gbk编码下面是这个配置文件的例子 1 2 3 4 5 6 7 8 9 10 11 [pytest] # 命令行参数用空格分隔 addopts -vs # 测试用例文件夹可以自己配置 testpaths ./test_demo # 配置测试搜索的模块文件名称 python_files test.py # 配置测试搜索的类名 python_classes Test # 配置搜索的函数名 python_functions test 分组执行
定义三个组冒烟smoke用户管理user_manager作业管理worker_manager
目前有几个用例给加个分组的装饰器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import pytest class TestDemo: pytest.mark.somke def test_case1(self): print(1) pytest.mark.user_manage def test_case2(self): print(2) pytest.mark.worker_manage def test_case3(self): print(3)
配置文件中加入分组信息 1 2 3 4 markers smoke:冒烟测试 user_manage:用户管理 worker_manage:作业管理
运行 运行多组 1 2 3 4 import pytest if name main: pytest.main([-vs, -m smoke or usermanage])
运行单组 1 2 3 4 import pytest if name main: pytest.main([-vs, -m smoke]) 忽略执行
无条件忽略
直接使用装饰器pytest.mark.skip(reason“原因填写”)
有条件忽略
使用装饰器pytest.mark.skipif(条件, 原因)
例子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import pytest class TestDemo: age 18 pytest.mark.smoke def test_case1(self): print(1) pytest.mark.usermanage pytest.mark.skipif(age 18, 未成年) def test_case2(self): print(2) pytest.mark.workermanage pytest.mark.skip(reason原因填写) def test_case3(self): print(3)
pytest中的前后置处理
为什么需要前后置比如执行用例前需要做一些准备工作比如打开浏览器在执行用例后需要一些后置工作比如关闭浏览器
模块级别
在每个模块执行前会调用setup_module方法在每个模块执行后会使用teardown_module方法。 例子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import pytest def setup_module(): print(模块用例前执行) def teardown_module(): print(模块用例后执行) class TestDemo: def test_case1(self): print(1) def test_case2(self): print(2) def test_case3(self): print(3) class TestDemo2: def test_case4(self): print(4)
结果 1 2 3 4 5 6 7 8 9 test_demo/test_demo2.py::TestDemo::test_case1 模块用例前执行 1 PASSED test_demo/test_demo2.py::TestDemo::test_case2 2 PASSED test_demo/test_demo2.py::TestDemo::test_case3 3 PASSED test_demo/test_demo2.py::TestDemo2::test_case4 4 PASSED模块用例后执行
类级别
类级别函数 setup_class/teardown_class 对类有效位于类中在测试类中前后调用一次。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class TestDemo: def setup_class(self): print(类级别前置) def test_case1(self): print(1) def test_case2(self): print(2) def test_case3(self): print(3) def teardown_class(self): print(类级别后置) 1 2 3 4 5 6 7 8 9 test_demo/test_demo2.py::TestDemo::test_case1 模块用例前执行 类级别前置 1 PASSED test_demo/test_demo2.py::TestDemo::test_case2 2 PASSED test_demo/test_demo2.py::TestDemo::test_case3 3 PASSED类级别后置 模块用例后执行
方法级别
方法级别函数 setup_method/teardown_method和setup/teardown对类有效也位于类中这两个效果一样在测试类中每个测试方法前后调用一次。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class TestDemo: def setup_method(self): print(方法级别前置) def test_case1(self): print(1) def test_case2(self): print(2) def test_case3(self): print(3) def teardown_method(self): print(方法级别后置) 1 2 3 4 5 6 7 8 9 10 11 test_demo/test_demo3.py::TestDemo::test_case1 方法级别前置 PASSED方法级别后置 test_demo/test_demo3.py::TestDemo::test_case2 方法级别前置 PASSED方法级别后置 test_demo/test_demo3.py::TestDemo::test_case3 方法级别前置 PASSED方法级别后置
部分用例的前后置 pytest.fixture装饰器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import pytest pytest.fixture(scopefunction, params[1, 2, 3], autouseFalse, idsNone, namenew_name) def my_feature(request): i request.param print(前置) yield i print(后置) class TestDemo: def test_case1(self, new_name): print(new_name) print(1)
结果 test_demo/test_demo4.py::TestDemo::test_case1[1] 前置 1 1 PASSED后置 test_demo/test_demo4.py::TestDemo::test_case1[2] 前置 2 1 PASSED后置 test_demo/test_demo4.py::TestDemo::test_case1[3] 前置 3 1 PASSED后置 scope表示作用域params表示参数化与yield使用会调用len(params)次用例如例子所示一般用于数据驱动autouse默认使用一般设置为falseidsparams参数化时给每个参数起名字name给该方法取别名
pytest.fixtureconftest
fixture为session级别是可以跨.py模块调用的也就是当我们有多个.py文件的用例的时候如果多个用例只需调用一次fixture那就可以设置为scope“session”并且写到conftest.py文件里。
conftest.py文件名称时固定的pytest会自动识别该文件。放到项目的根目录下就可以全局调用了如果放到某个package下那就在改package内有效。
例子 在包下创建conftest.py注意该配置只在本包生效 和之前一样使用 结果还是和之前一样。
pytest生成测试报告
pytest-html插件生成报告 1 pytest -vs –html ./report/report.html
参数化与数据驱动
主要用的装饰器是pytest.mark.parametrize(argnames, argvalues)
不带名字数据驱动 1 2 3 4 5 6 import pytest class TestDemo: pytest.mark.parametrize(args,[(4399, AAAA), (2012, BBBB)]) def test_case1(self, args): print(args)
结果 test_demo/test_demo4.py::TestDemo::test_case1args0 PASSED test_demo/test_demo4.py::TestDemo::test_case1args1 PASSED 带名字的数据驱动 1 2 3 4 5 6 import pytest class TestDemo: pytest.mark.parametrize(arg1,arg2, [(4399, AAAA), (2012, BBBB)]) def test_case1(self, arg1, arg2): print(arg1, arg2)
结果 test_demo/test_demo4.py::TestDemo::test_case1[4399-AAAA] 4399 AAAA PASSED test_demo/test_demo4.py::TestDemo::test_case1[2012-BBBB] 2012 BBBB PASSED 最后感谢每一个认真阅读我文章的人看着粉丝一路的上涨和关注礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走 希望能帮助到你【100%无套路免费领取】
- 上一篇: 男女做暧视频网站免费创意产品设计图
- 下一篇: 男女做污污的网站有什么可以接单做设计的网站
相关文章
-
男女做暧视频网站免费创意产品设计图
男女做暧视频网站免费创意产品设计图
- 技术栈
- 2026年03月21日
-
男女明星直接做的视频网站网页搜索屏蔽广告
男女明星直接做的视频网站网页搜索屏蔽广告
- 技术栈
- 2026年03月21日
-
男科治疗价目表2017织梦网站怎么做seo
男科治疗价目表2017织梦网站怎么做seo
- 技术栈
- 2026年03月21日
-
男女做污污的网站有什么可以接单做设计的网站
男女做污污的网站有什么可以接单做设计的网站
- 技术栈
- 2026年03月21日
-
男人和女人做不可描述的事情的网站郑州论坛官网
男人和女人做不可描述的事情的网站郑州论坛官网
- 技术栈
- 2026年03月21日
-
男生和男生男生做的漫画网站关于化妆品网站成功案例
男生和男生男生做的漫画网站关于化妆品网站成功案例
- 技术栈
- 2026年03月21日
