用途:pytest 是 Python 测试框架,用于编写和运行单元测试与集成测试,支持依赖注入、数据驱动测试、异步测试及与 FastAPI、Django、Flask 等框架集成。
主要能力:fixture 系统用于依赖注入,支持 function、class、module、session 多种作用域以及 yield 式 setup/teardown 和 fixture 依赖;parametrize 支持数据驱动测试与间接参数化;内置丰富断言自省,无需 self.assertEqual;支持 pytest.raises、pytest.approx 等断言辅助;提供 skip、skipif、xfail 及自定义 markers;插件生态包含 pytest-cov、pytest-asyncio、pytest-mock、pytest-django,pytest-xdist 支持并行执行;具备测试发现、组织和详细失败报告能力。
使用方式:安装命令 pip install pytest;常用插件 pip install pytest pytest-cov pytest-asyncio pytest-mock;FastAPI 可 pip install pytest httpx pytest-asyncio;Django 可 pip install pytest pytest-django;异步数据库可 pip install pytest-asyncio aiosqlite。命令行运行 pytest,可用 pytest -v、pytest -s、pytest test_math.py、pytest test_math.py::test_add 指定输出或范围,用 pytest -m unit 等按标记筛选。FastAPI 使用 fastapi.testclient.TestClient 或 httpx.AsyncClient,Django 使用 pytest.mark.django_db。
适用场景:适合单元测试、集成测试、API 测试、Django 模型与视图测试、异步测试和 mock 替换外部依赖。
限制:资料未提供官方价格、收费模式、性能基准或与其他框架的对比数据。