pytest框架快速进阶篇-pytest前置和pytest后置,skipif跳过用例

**一、Pytest的前置和后置方法

1.Pytest可以集成unittest实现前置和后置

复制代码
importunittestimportpytestclassTestCase(unittest.TestCase):defsetUp(self)->None:print('unittest每个用例前置')deftearDown(self)->None:print('unittest每个用例后置')@classmethoddefsetUpClass(cls)->None:print('unittest所有用例的前置,所有用例之前只执行一次!')@classmethoddeftearDownClass(cls)->None:print('unittest所有用例的后置,所有用例执行之后只执行一次')deftest_03(self):print('测试用例三')deftest04(self):print('测试用例四')if__name__ =='__main__':
    pytest.main(['-s','pytest-demo.py'])

注意:setUpClass和tearDownClass需要用@classmethod装饰器装饰。

2.Pytest前置和后置

复制代码
importpytestclassTestCase:defsetup_class(self):print('Pytest所有用例的前置,所有用例之前只执行一次!')defteardown_class(self):print('Pytest所有用例的后置,所有用例执行之后只执行一次')defsetup(self):print('Pytest每个用例前置')defteardown(self):print('Pytest每个用例后置')deftest_03(self):print('测试用例三')deftest04(self):print('测试用例四')if__name__ =='__main__':
    pytest.main(['-s','pytest-demo.py'])

注意:setup、teardown、setup_class、teardown_class都是小写!

二、跳过用例

使用方法:

@pytest.mark.skipif(2>1,reason='当条件不True时跳过')

使用命令:pytest -vv  执行结果显示更清楚。

这可能是B站最详细的pytest自动化测试框架教程,整整100小时,全程实战!!!

相关推荐
丿罗小黑1 天前
Pytest项目_day06(requests中Session的用法)
pytest
测试开发技术2 天前
软件测试中,pytest 如何运行多个文件或整个目录?
自动化测试·pytest·接口测试·面试题
鱼骨不是鱼翅4 天前
自动化框架pytest
运维·自动化·pytest
2025年一定要上岸8 天前
【pytest高阶】-2- 内置hook插件扩展机制和定制开发
pytest
2025年一定要上岸10 天前
【pytest高阶】源码的走读方法及插件hook
运维·前端·python·pytest
惜.己11 天前
pytest中使用ordering控制函数的执行顺序
开发语言·python·pytest
惜.己12 天前
pytest中使用skip跳过某个函数
开发语言·python·测试工具·pytest
程序员小远13 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
惜.己15 天前
pytest简单使用和生成测试报告
开发语言·python·pytest
better-tomorrow15 天前
pytest-log
pytest