--
:
--
:
--
第三阶段:高级特性与插件系统
最后更新于:
第三阶段:高级特性与插件系统
一、用例组织
1.1 测试类
1class TestCalculator:
2 def test_add(self):
3 # 测试加法
4
5 def test_subtract(self):
6 # 测试减法1.2 测试模块
1# test_calculator.py - 一个测试模块
2def test_add():
3 pass
4
5def test_subtract():
6 pass1.3 测试包
1tests/
2├── __init__.py
3├── test_calculator.py
4├── test_utils.py
5└── api/
6 ├── __init__.py
7 └── test_auth.py1.4 自定义标记
1@pytest.mark.smoke
2def test_login():
3 pass
4
5@pytest.mark.regression
6def test_checkout():
7 pass1.5 标记配置
在pytest.ini中注册标记:
1[pytest]
2markers =
3 smoke: 冒烟测试
4 regression: 回归测试
5 performance: 性能测试1.6 标记筛选
1pytest -m smoke # 只运行smoke测试
2pytest -m "not smoke" # 排除smoke测试
3pytest -m "smoke or regression" # 运行smoke或regression测试二、插件系统
2.1 常用插件
| 插件 | 功能 | 安装命令 |
|---|---|---|
pytest-html | 生成HTML测试报告 | pip install pytest-html |
pytest-xdist | 并行执行测试 | pip install pytest-xdist |
pytest-cov | 代码覆盖率统计 | pip install pytest-cov |
pytest-mock | Mock支持 | pip install pytest-mock |
allure-pytest | 生成Allure报告 | pip install allure-pytest |
pytest-base-url | 配置基础URL | pip install pytest-base-url |
2.2 HTML报告生成
1pytest --html=report.html -v2.3 并行执行
1# 使用4个CPU核心并行执行
2pytest -n 42.4 代码覆盖率
1# 统计代码覆盖率
2pytest --cov=src --cov-report=html
3
4# 查看未覆盖的代码行
5pytest --cov=src --cov-report=term-missing三、conftest.py
3.1 什么是conftest.py?
conftest.py是pytest的配置文件,用于定义fixture和共享配置。pytest会自动发现项目中的conftest.py文件。
3.2 作用域
| 位置 | 作用域 |
|---|---|
| 项目根目录 | 全局作用域 |
| 测试子目录 | 该目录及其子目录 |
3.3 示例
1# tests/conftest.py
2import pytest
3
4@pytest.fixture(scope="session")
5def api_client():
6 session = requests.Session()
7 yield session
8 session.close()
9
10@pytest.fixture
11def test_user(api_client):
12 user = create_test_user()
13 yield user
14 delete_test_user(user)四、测试跳过与预期失败
4.1 跳过测试
1@pytest.mark.skip(reason="暂不执行")
2def test_feature_not_implemented():
3 pass
4
5@pytest.mark.skipif(sys.version_info < (3, 8), reason="需要Python 3.8+")
6def test_modern_feature():
7 pass4.2 预期失败
1@pytest.mark.xfail(reason="已知bug")
2def test_known_bug():
3 assert False # 预期会失败
4
5@pytest.mark.xfail(strict=True) # 严格模式,失败时标记为失败
6def test_must_pass():
7 assert True五、测试排序
5.1 使用pytest-ordering插件
1pip install pytest-ordering1@pytest.mark.order(1)
2def test_first():
3 pass
4
5@pytest.mark.order(2)
6def test_second():
7 pass六、示例代码
6.1 测试类示例
1# tests/test_class_demo.py
2import pytest
3from src.calculator import Calculator
4
5class TestCalculator:
6 def setup_method(self):
7 self.calc = Calculator()
8
9 def teardown_method(self):
10 del self.calc
11
12 def test_add(self):
13 assert self.calc.add(2, 3) == 5
14
15 def test_subtract(self):
16 assert self.calc.subtract(5, 2) == 36.2 标记示例
1# tests/test_markers_demo.py
2import pytest
3
4@pytest.mark.smoke
5def test_is_prime_basic():
6 assert is_prime(2) == True
7
8@pytest.mark.regression
9def test_is_prime_edge_cases():
10 assert is_prime(1) == False七、易错点与注意事项
7.1 插件常见错误
- 插件未安装:使用插件前需要先安装
- 版本不兼容:插件版本与pytest版本不兼容
- 配置错误:需要在
pytest.ini中正确配置
7.2 conftest.py常见错误
- 命名错误:文件名必须是
conftest.py - 位置错误:放在错误的目录下
- 循环依赖:fixture之间相互依赖
7.3 标记常见错误
- 未注册标记:在
pytest.ini中未注册自定义标记会产生警告 - 标记滥用:过多的标记会使测试难以维护
- 标记冲突:不同层级的标记定义不一致
八、小结
本阶段学习了pytest的高级特性,包括:
- 测试类、测试模块、测试包的组织方式
- 自定义标记和筛选
- 常用插件的使用
- conftest.py的配置
- 测试跳过和预期失败
📡
👤
作者:
阿海
🌐
版权:
本站文章除特别声明外,均采用
CC BY-NC-SA 4.0
协议,转载请注明来自
阿海 Blog!
- 01JMeter界面详解 2026-07-11
- 02性能测试学习路线 2026-07-11
- 03JMeter线程组配置详解 2026-07-11