SuperAPI AI 工具集

pytest

测试与质量

查看项目

更新于 10-16 06:45

技能介绍

用途:pytest 是面向 Python 的测试框架,适合从简单单元测试扩展到复杂的功能测试。

主要能力:直接用 assert 编写断言;通过 @pytest.fixture 定义测试夹具,支持 setup/teardown(yield 前后)及 function、module、session 等作用域;用 @pytest.mark.parametrize 做参数化与组合测试;用 @pytest.mark.asyncio 配合 pytest-asyncio 编写异步测试;在 conftest.py 中共享夹具,支持测试类;用 monkeypatch 修改环境变量和属性,tmp_path 提供临时目录;用自定义 marker 标记并按 marker 筛选测试(如 pytest -m unit、pytest -m "not slow");用 pytest.raises 测试异常,可搭配 match 匹配消息。

使用方式:pip install pytest 安装;运行全部测试用 pytest,加 -v 输出详细结果,可指定文件 pytest test_example.py,或指定函数 pytest test_example.py::test_addition。异步测试需 pip install pytest-asyncio;API 测试可使用对应框架的测试客户端(如 httpx)。

适用场景:Python 项目单元测试、功能测试、API 测试、数据库测试、异步代码测试及错误处理测试。

限制:需要 Python 3.7+;异步与部分 API 测试需额外安装插件或依赖;资料未提供更多版本、收费、性能与平台细节。