Pytest Code Review
Quick Reference
references/fixtures.md |
| @pytest.mark.parametrize, DRY patterns |
references/parametrize.md |
| AsyncMock tracking, patch patterns, when to mock |
references/mocking.md |
Review Checklist
- - [ ] Test functions are
async def test_* for async code under test - [ ] AsyncMock used for async dependencies, not Mock
- [ ] All async mocks and coroutines are awaited
- [ ] Fixtures in conftest.py for shared setup
- [ ] Fixture scope appropriate (function, class, module, session)
- [ ] Yield fixtures have proper cleanup in finally block
- [ ] @pytest.mark.parametrize for similar test cases
- [ ] No duplicated test logic across multiple test functions
- [ ] Mocks track calls properly (assertcalledonce_with)
- [ ] patch() targets correct location (where used, not defined)
- [ ] No mocking of internals that should be tested
- [ ] Test isolation (no shared mutable state between tests)
When to Load References
- - Reviewing async test functions → async-testing.md
- Reviewing fixtures or conftest.py → fixtures.md
- Reviewing similar test cases → parametrize.md
- Reviewing mocks and patches → mocking.md
Review Questions
- 1. Are all async functions tested with async def test_*?
- Are fixtures properly scoped with appropriate cleanup?
- Can similar test cases be parametrized to reduce duplication?
- Are mocks tracking calls and used at the right locations?
技能名称: pytest-code-review
详细描述:
Pytest 代码审查
快速参考
references/fixtures.md |
| @pytest.mark.parametrize、DRY 模式 |
references/parametrize.md |
| AsyncMock 追踪、patch 模式、何时模拟 |
references/mocking.md |
审查清单
- - [ ] 测试函数使用 async def test* 来测试异步代码
- [ ] 对异步依赖使用 AsyncMock,而非 Mock
- [ ] 所有异步模拟和协程都被 await 等待
- [ ] 共享设置使用 conftest.py 中的夹具
- [ ] 夹具作用域适当(函数、类、模块、会话)
- [ ] 带 yield 的夹具在 finally 块中有正确的清理
- [ ] 对相似测试用例使用 @pytest.mark.parametrize
- [ ] 多个测试函数之间没有重复的测试逻辑
- [ ] 模拟正确追踪调用(assertcalledoncewith)
- [ ] patch() 定位正确的位置(在使用处,而非定义处)
- [ ] 不模拟应被测试的内部实现
- [ ] 测试隔离(测试之间没有共享的可变状态)
何时加载参考文档
- - 审查异步测试函数 → async-testing.md
- 审查夹具或 conftest.py → fixtures.md
- 审查相似测试用例 → parametrize.md
- 审查模拟和补丁 → mocking.md
审查问题
- 1. 所有异步函数是否都使用 async def test_* 进行测试?
- 夹具是否具有适当的作用域和正确的清理?
- 相似测试用例能否参数化以减少重复?
- 模拟是否追踪调用并在正确的位置使用?