头像
淇迹时刻
-- : -- : --
切换主题色
-- : -- : --

第三阶段:高级特性与插件系统

最后更新于:

第三阶段:高级特性与插件系统

一、用例组织

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    pass

1.3 测试包

1tests/
2├── __init__.py
3├── test_calculator.py
4├── test_utils.py
5└── api/
6    ├── __init__.py
7    └── test_auth.py

1.4 自定义标记

1@pytest.mark.smoke
2def test_login():
3    pass
4
5@pytest.mark.regression  
6def test_checkout():
7    pass

1.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-mockMock支持pip install pytest-mock
allure-pytest生成Allure报告pip install allure-pytest
pytest-base-url配置基础URLpip install pytest-base-url

2.2 HTML报告生成

1pytest --html=report.html -v

2.3 并行执行

1# 使用4个CPU核心并行执行
2pytest -n 4

2.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    pass

4.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-ordering
1@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) == 3

6.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 插件常见错误

  1. 插件未安装:使用插件前需要先安装
  2. 版本不兼容:插件版本与pytest版本不兼容
  3. 配置错误:需要在pytest.ini中正确配置

7.2 conftest.py常见错误

  1. 命名错误:文件名必须是conftest.py
  2. 位置错误:放在错误的目录下
  3. 循环依赖:fixture之间相互依赖

7.3 标记常见错误

  1. 未注册标记:在pytest.ini中未注册自定义标记会产生警告
  2. 标记滥用:过多的标记会使测试难以维护
  3. 标记冲突:不同层级的标记定义不一致

八、小结

本阶段学习了pytest的高级特性,包括:

  • 测试类、测试模块、测试包的组织方式
  • 自定义标记和筛选
  • 常用插件的使用
  • conftest.py的配置
  • 测试跳过和预期失败
最新文章