SuperAPI AI 工具集

pytest-patterns

测试与质量

查看项目

更新于 10-16 03:47

技能介绍

用途:这是一个面向 Python 的 pytest 测试技能,用于在编写、审查或调试 pytest 测试时提供系统化指导,覆盖 fixture、parametrize、marker、conftest、插件、mocking 与高级测试模式。

主要能力:

  1. 项目结构与配置:提供 src/tests 分层结构,以及 pytest.ini、pyproject.toml 中 testpaths、python_files、addopts、markers、coverage 等配置写法。
  2. Fixture:支持基础 fixture、function/class/module/session 作用域、工厂 fixture、yield 方式 setup/teardown,以及 tmp_path、事务回滚、mock server 等用法。
  3. Parametrize:支持单参数与多参数数据驱动测试、pytest.param 自定义 id、叠加多个 parametrize 装饰器。
  4. Markers:内置 slow、integration、smoke、unit 等自定义 marker,以及 skip、skipif、xfail 的使用。
  5. Mocking:借助 pytest-mock 的 mocker 进行 patch、side_effect、时间 mock 等操作。
  6. Conftest:按层级放置共享 fixture,包含 autouse、环境重置、API 客户端与认证客户端等模式。
  7. 异常、临时文件与目录:pytest.raises、match、异常属性断言,以及 tmp_path 处理文件读写。

使用方式:安装 pytest-mock 等依赖后,按约定命名测试文件与测试函数,运行 pytest 命令;常用命令包括 pytest、pytest tests/unit/test_user_service.py、pytest -m smoke、pytest -m "not slow"、pytest --cov=src --cov-report=html、pytest -n auto(需 pytest-xdist)、pytest -x、pytest --lf、pytest -v。

适用场景:Python 项目的单元测试、集成测试、冒烟测试、API 测试、数据校验与回归测试,尤其适合希望用 fixture 和 parametrize 减少重复代码、用 marker 分类执行测试的场景。

限制:资料未说明是否收费、支持的 pytest 版本范围、与其他测试框架的兼容细节或效果承诺,暂未提供。具体项目需结合自身目录、依赖和运行环境调整配置。