SuperAPI AI 工具集

pytest-mastery

测试与质量

查看项目

更新于 10-15 17:12

技能介绍

用途:指导在 Python 项目中用 pytest 编写与运行测试,并以 uv 作为包管理器。主要能力:测试自动发现(文件 test_.py 或 test.py,函数 test,类 Test 且无 init,类内方法 test_);fixture 提供可复用 setup/teardown,支持 function、class、module、session 四种作用域,可在 tests/conftest.py 中共享;@pytest.mark.parametrize 参数化多组输入;常用选项如 -v、-vv、-q、-x、--lf、--ff、-k、-m、--tb、-s、--durations、-n auto(并行需 pytest-xdist);覆盖率可用 --cov、--cov-report=html、--cov-fail-under;标记支持 skip、skipif、xfail 等;可在 pyproject.toml 的 [tool.pytest.ini_options] 及 [tool.coverage.] 中配置测试路径、文件与函数命名、默认参数、标记及覆盖率排除项。用法:安装用 uv add --dev pytest,覆盖率加 uv add --dev pytest-cov,异步(如 FastAPI)加 uv add --dev pytest-asyncio httpx;运行示例 uv run pytest、uv run pytest -v、uv run pytest tests/test_example.py::test_function_name、uv run pytest -k "pattern"、uv run pytest --cov=src --cov-report=html。项目结构示例为 src/ 与 tests/(含 conftest.py、test_unit.py、integration/test_api.py)及 pyproject.toml。FastAPI 测试可参考 references/fastapi-testing.md,涉及 TestClient、httpx 异步、数据库 fixture、依赖覆盖与认证测试。调试可用 --tb=long、--pdb、-l、--lf。限制:资料未提供版本要求、价格、平台及其他未提及细节,暂未提供。