用途:为项目搭建 pytest 配置,确保测试发现快速、隔离正确、跨环境行为一致。适用于“configure pytest”“setup test discovery”“create conftest”“configure coverage”等场景;不用于编写测试本身、调试失败或 mock 策略。
主要能力:在 pyproject.toml 的 [tool.pytest.ini_options] 中配置测试发现(testpaths、python_files、python_classes、python_functions、minversion);异步支持(asyncio_mode = "auto"、asyncio_default_fixture_loop_scope);自定义标记(unit、integration、e2e、slow、smoke、skip_ci);覆盖率(--cov、--cov-report、--cov-fail-under=80,[tool.coverage.run]/report/html);日志(log_cli、log_file);插件(pytest_asyncio、pytest_cov)。
使用方式:按步骤配置发现、异步、标记、覆盖率、日志,创建根级与领域级 conftest.py 共享 fixture 与 mock,按环境区分数据库 URL,并用 pytest -m unit、pytest -m "not slow"、pytest -n auto 等命令运行。
适用场景:需要统一测试配置、隔离环境变量、按标记筛选测试、生成覆盖率报告的项目。
限制:要求 Python 3.11+、pytest >= 7.0、pytest-asyncio >= 0.20.0、pytest-cov >= 4.0、pytest-mock >= 3.10;具体项目适配细节暂未提供。