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

相关推荐
我的xiaodoujiao3 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 24--数据驱动--参数化处理 Excel 文件 1
python·学习·测试工具·pytest
西游音月6 天前
(2)pytest+Selenium自动化测试-环境准备
selenium·测试工具·pytest
我的xiaodoujiao6 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 23--数据驱动--参数化处理 Yaml 文件
python·学习·测试工具·pytest
我的xiaodoujiao9 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 22--数据驱动--参数化处理 Json 文件
python·学习·测试工具·pytest
胜天半月子10 天前
Python自动化测试 | 快速认识并了解pytest的基本使用
服务器·python·pytest
北珣.14 天前
自动化框架pytest基础
自动化·pytest
程序员杰哥15 天前
Pytest之收集用例规则与运行指定用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
学习3人组15 天前
Python + requests + pytest + allure + Jenkins 构建完整的接口自动化测试框架
python·jenkins·pytest
shao91851618 天前
Gradio全解14——使用Gradio构建MCP的服务器与客户端(4)——Python包命令:uv与uvx实战
pytest·uv·1024程序员节·npx·uvx·uv pip·ruff
我的xiaodoujiao19 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 20--PO(POM) 设计模式和用例撰写
python·学习·测试工具·设计模式·pytest