用途:pytest-writer 用于编写高质量、可维护的 Python 测试用例,覆盖创建测试文件、重构优化测试、调试失败测试、使用 fixtures、参数化与覆盖率分析等场景。
主要能力:支持简单 assert 断言并输出详细失败信息;使用 @pytest.fixture 管理测试资源与依赖,支持 conftest.py 共享 fixtures;使用 @pytest.mark.parametrize 减少重复用例;用 pytest.raises 测试异常并检查异常信息;通过 @pytest.mark.skip、skipif、xfail 控制跳过与预期失败;推荐 AAA(Arrange-Act-Assert)模式与测试隔离等最佳实践。
用法:测试文件命名 test_.py 或 test.py,测试函数命名 test,测试类命名 Test。运行命令:pytest;指定文件 pytest tests/test_auth.py;指定函数 pytest tests/test_auth.py::test_login;按模式匹配 pytest -k "login";详细输出 pytest -v;显示 print 用 pytest -s;首个失败停止 pytest -x;重跑上次失败 pytest --lf;覆盖率 pytest --cov=src,HTML 报告 pytest --cov=src --cov-report=html;调试 pytest --pdb,也可在代码中 import pdb; pdb.set_trace()。
适用场景:单元测试、API/数据库测试组织、测试重构与覆盖率分析。
限制:资料未提供安装方式、版本要求及具体收费信息,暂未提供。