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小时,全程实战!!!

相关推荐
思则变3 天前
[Pytest][Part 1]Pytest 自动化测试框架
pytest
思则变4 天前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
思则变5 天前
[Pytest][Part 3]检测python package状态
pytest
cooldream200919 天前
pytest 框架详解与实战指南
pytest·测试
慕城南风19 天前
【pytest进阶】Pytest之conftest详解
pytest
编程小白gogogo20 天前
AI自动化测试速成(Pytest框架)
pytest
慕城南风20 天前
【pytest进阶】pytest详解及进阶使用
linux·服务器·pytest
Tom Boom1 个月前
Pytest断言全解析:掌握测试验证的核心艺术
自动化测试·python·测试开发·pytest
不要一直敲门1 个月前
初学 pytest 记录
pytest