pytest-code-review
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePytest Code Review
Pytest代码审核
Quick Reference
快速参考
| Issue Type | Reference |
|---|---|
| async def test_*, AsyncMock, await patterns | references/async-testing.md |
| conftest.py, factory fixtures, scope, cleanup | references/fixtures.md |
| @pytest.mark.parametrize, DRY patterns | references/parametrize.md |
| AsyncMock tracking, patch patterns, when to mock | references/mocking.md |
| 问题类型 | 参考文档 |
|---|---|
| async def test_*、AsyncMock、await模式 | references/async-testing.md |
| conftest.py、工厂fixtures、作用域、清理 | references/fixtures.md |
| @pytest.mark.parametrize、DRY模式 | references/parametrize.md |
| AsyncMock跟踪、patch模式、模拟时机 | references/mocking.md |
Review Checklist
审核检查清单
- Test functions are for async code under test
async def 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 (assert_called_once_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)
- 针对被测异步代码,测试函数使用定义
async def test_* - 异步依赖使用AsyncMock而非Mock
- 所有异步模拟和协程都已使用await
- 在conftest.py中定义共享初始化的fixtures
- Fixture的作用域设置恰当(function、class、module、session)
- Yield类型的fixtures在finally块中正确实现清理逻辑
- 相似测试案例使用@pytest.mark.parametrize
- 多个测试函数间无重复的测试逻辑
- 模拟对象正确跟踪调用(使用assert_called_once_with)
- patch()的目标位置正确(使用处而非定义处)
- 未对需要测试的内部逻辑进行模拟
- 测试隔离(测试间无共享可变状态)
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
- 审核异步测试函数 → async-testing.md
- 审核fixtures或conftest.py → fixtures.md
- 审核相似测试案例 → parametrize.md
- 审核模拟和patch → mocking.md
Review Questions
审核问题
- 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?
- 所有异步函数是否都使用async def test_*进行测试?
- Fixtures是否设置了恰当的作用域和清理逻辑?
- 相似测试案例是否可通过参数化减少重复?
- 模拟对象是否正确跟踪调用并在合适位置使用?