pytest-code-review

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pytest Code Review

Pytest代码审核

Quick Reference

快速参考

Issue TypeReference
async def test_*, AsyncMock, await patternsreferences/async-testing.md
conftest.py, factory fixtures, scope, cleanupreferences/fixtures.md
@pytest.mark.parametrize, DRY patternsreferences/parametrize.md
AsyncMock tracking, patch patterns, when to mockreferences/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
    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 (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

审核问题

  1. Are all async functions tested with async def test_*?
  2. Are fixtures properly scoped with appropriate cleanup?
  3. Can similar test cases be parametrized to reduce duplication?
  4. Are mocks tracking calls and used at the right locations?
  1. 所有异步函数是否都使用async def test_*进行测试?
  2. Fixtures是否设置了恰当的作用域和清理逻辑?
  3. 相似测试案例是否可通过参数化减少重复?
  4. 模拟对象是否正确跟踪调用并在合适位置使用?