| name | python-testing |
| description | Use when writing tests, setting up pytest, implementing TDD, creating fixtures, or mocking dependencies. Do NOT use for general patterns (use python-patterns) or style config (use python-code-style). |
| paths | **/*.py, **/pyproject.toml, **/pytest.ini, **/conftest.py |
Python Testing Patterns
์ค์ ์ฆ์ ์ง์ ๊ณผ ํ๋ก์ ํธ ์ปจ๋ฒค์
๋ง ๋ด๋๋ค. pytest API ์ผ๋ฐ ์ง์์ ๋ชจ๋ธ์ ์ด๋ฏธ ์์.
CRITICAL Rules
- Test behavior, not implementation โ ๋ด๋ถ ๊ตฌํ์ด ์๋ ์
์ถ๋ ฅ/๋ถ์ํจ๊ณผ ๊ฒ์ฆ
- Mock at boundaries only โ ์ธ๋ถ ์์คํ
(DB, API, filesystem)๋ง mock, ๋ด๋ถ ๋ก์ง์ ์ค์ ์คํ
- Patch where you USE, not where you define โ
patch("myapp.service.requests.get") NOT patch("requests.get")
- ALWAYS
autospec=True on mocks โ API ๋ถ์ผ์น ์กฐ๊ธฐ ๋ฐ๊ฒฌ
- PREFER
pytest.raises(match=...) โ ์์ธ ํ์
+ ๋ฉ์์ง ํจ๊ป ๊ฒ์ฆ
- NEVER test third-party code โ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋์ํ๋์ง ํ
์คํธํ์ง ๋ง๋ผ
- 80%+ coverage, critical paths 100% โ
pytest --cov --cov-fail-under=80
pytest Configuration (ํ๋ก์ ํธ ์ปจ๋ฒค์
)
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = ["--strict-markers", "--cov=src", "--cov-report=term-missing", "--cov-fail-under=80", "-x", "--tb=short"]
markers = ["slow: marks tests as slow", "integration: integration tests"]
[tool.coverage.report]
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:", "raise NotImplementedError", "@abstractmethod"]
ํ
์คํธ ๋ค์ด๋ฐ: test_{method}_{scenario}_{expected_result} โ ์: test_create_user_duplicate_email_raises_conflict
Gotchas
- โ
patch('module.Class') (์ ์ ์์น) โ patch('consumer_module.Class') (์ฌ์ฉ ์์น)
- โ
autospec=True ๋๋ฝ โ ์กด์ฌํ์ง ์๋ ๋ฉ์๋ ํธ์ถํด๋ ํต๊ณผ
- โ
assert mock.called โ mock.assert_called_once_with(expected) ์ฌ์ฉ
- โ session-scope fixture์ mutable state โ ํ
์คํธ ๊ฐ ์ค์ผ, ๊ฐํ์ ์คํจ
- โ async test๊ฐ ์กฐ์ฉํ skip๋จ โ pytest-asyncio
mode = "auto" ์ค์ ๋๋ฝ
- โ ๊ฐํ์ ์คํจ ์์ธ ์ถ์ โ
pytest-randomly๋ก ์์ ๋ฌด์์ํ ํ ์ฌํ
Cross-References
| Topic | Skill |
|---|
| Python ํจํด, ํ์
ํํธ, ๋์์ฑ | python-patterns |
| Ruff, mypy, formatting, naming | python-code-style |
| TDD methodology (general) | test-driven-development superpowers |