@grimoire-cc/cli 0.13.2 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/commands/update.d.ts.map +1 -1
  2. package/dist/commands/update.js +14 -0
  3. package/dist/commands/update.js.map +1 -1
  4. package/dist/enforce.d.ts +3 -1
  5. package/dist/enforce.d.ts.map +1 -1
  6. package/dist/enforce.js +18 -6
  7. package/dist/enforce.js.map +1 -1
  8. package/dist/setup.d.ts.map +1 -1
  9. package/dist/setup.js +47 -0
  10. package/dist/setup.js.map +1 -1
  11. package/dist/summary.d.ts.map +1 -1
  12. package/dist/summary.js +9 -0
  13. package/dist/summary.js.map +1 -1
  14. package/package.json +1 -1
  15. package/packs/dev-pack/agents/grimoire.tdd-specialist.md +194 -27
  16. package/packs/dev-pack/grimoire.json +9 -42
  17. package/packs/dev-pack/skills/grimoire.conventional-commit/SKILL.md +68 -47
  18. package/packs/dotnet-pack/agents/grimoire.csharp-coder.md +110 -113
  19. package/packs/dotnet-pack/grimoire.json +23 -5
  20. package/packs/dotnet-pack/skills/grimoire.unit-testing-dotnet/SKILL.md +252 -0
  21. package/packs/{dev-pack/skills/grimoire.tdd-specialist → dotnet-pack/skills/grimoire.unit-testing-dotnet}/reference/anti-patterns.md +78 -0
  22. package/packs/dotnet-pack/skills/grimoire.unit-testing-dotnet/reference/tdd-workflow-patterns.md +259 -0
  23. package/packs/go-pack/grimoire.json +19 -0
  24. package/packs/go-pack/skills/grimoire.unit-testing-go/SKILL.md +256 -0
  25. package/packs/go-pack/skills/grimoire.unit-testing-go/reference/anti-patterns.md +244 -0
  26. package/packs/go-pack/skills/grimoire.unit-testing-go/reference/tdd-workflow-patterns.md +259 -0
  27. package/packs/python-pack/grimoire.json +19 -0
  28. package/packs/python-pack/skills/grimoire.unit-testing-python/SKILL.md +239 -0
  29. package/packs/python-pack/skills/grimoire.unit-testing-python/reference/anti-patterns.md +244 -0
  30. package/packs/python-pack/skills/grimoire.unit-testing-python/reference/tdd-workflow-patterns.md +259 -0
  31. package/packs/rust-pack/grimoire.json +29 -0
  32. package/packs/rust-pack/skills/grimoire.unit-testing-rust/SKILL.md +243 -0
  33. package/packs/rust-pack/skills/grimoire.unit-testing-rust/reference/anti-patterns.md +244 -0
  34. package/packs/rust-pack/skills/grimoire.unit-testing-rust/reference/tdd-workflow-patterns.md +259 -0
  35. package/packs/ts-pack/agents/grimoire.typescript-coder.md +36 -1
  36. package/packs/ts-pack/grimoire.json +27 -1
  37. package/packs/ts-pack/skills/grimoire.unit-testing-typescript/SKILL.md +255 -0
  38. package/packs/ts-pack/skills/grimoire.unit-testing-typescript/reference/anti-patterns.md +244 -0
  39. package/packs/ts-pack/skills/grimoire.unit-testing-typescript/reference/tdd-workflow-patterns.md +259 -0
  40. package/packs/dev-pack/skills/grimoire.tdd-specialist/SKILL.md +0 -248
  41. package/packs/dev-pack/skills/grimoire.tdd-specialist/reference/language-frameworks.md +0 -388
  42. package/packs/dev-pack/skills/grimoire.tdd-specialist/reference/tdd-workflow-patterns.md +0 -135
  43. package/packs/dotnet-pack/skills/grimoire.dotnet-unit-testing/SKILL.md +0 -293
  44. package/packs/dotnet-pack/skills/grimoire.dotnet-unit-testing/reference/anti-patterns.md +0 -329
  45. package/packs/dotnet-pack/skills/grimoire.dotnet-unit-testing/reference/framework-guidelines.md +0 -361
  46. package/packs/dotnet-pack/skills/grimoire.dotnet-unit-testing/reference/parameterized-testing.md +0 -378
  47. package/packs/dotnet-pack/skills/grimoire.dotnet-unit-testing/reference/test-organization.md +0 -476
  48. package/packs/dotnet-pack/skills/grimoire.dotnet-unit-testing/reference/test-performance.md +0 -576
  49. package/packs/dotnet-pack/skills/grimoire.dotnet-unit-testing/templates/tunit-template.md +0 -438
  50. package/packs/dotnet-pack/skills/grimoire.dotnet-unit-testing/templates/xunit-template.md +0 -303
@@ -0,0 +1,259 @@
1
+ # TDD Workflow Patterns
2
+
3
+ Guidance on the test-driven development process, when to apply it, and advanced techniques.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Canon TDD — Start with a Test List](#canon-tdd--start-with-a-test-list)
8
+ - [Red-Green-Refactor](#red-green-refactor)
9
+ - [Transformation Priority Premise](#transformation-priority-premise)
10
+ - [F.I.R.S.T. Principles](#first-principles)
11
+ - [London School vs Detroit School](#london-school-vs-detroit-school)
12
+ - [When to Use TDD](#when-to-use-tdd)
13
+ - [When TDD Is Less Effective](#when-tdd-is-less-effective)
14
+ - [BDD and ATDD Extensions](#bdd-and-atdd-extensions)
15
+ - [Advanced Techniques](#advanced-techniques)
16
+
17
+ ## Canon TDD — Start with a Test List
18
+
19
+ > Source: https://tidyfirst.substack.com/p/canon-tdd
20
+
21
+ Kent Beck's recommended starting point is not a single test but a **test list** — a written enumeration of all behaviors you intend to verify. This separates the creative work (what to test) from the mechanical work (write, make pass, refactor).
22
+
23
+ **Process:**
24
+ 1. Write down all behaviors the code needs — a flat list, not tests
25
+ 2. Pick the simplest item on the list
26
+ 3. Write one failing test for it
27
+ 4. Make it pass with the minimum code
28
+ 5. Refactor
29
+ 6. Cross off the item; repeat
30
+
31
+ **Why test order matters:** Starting with simpler behaviors forces simpler transformations (see TPP below) and lets the design emerge naturally. Jumping to complex cases early leads to over-engineered solutions. The test list keeps you focused and prevents scope creep.
32
+
33
+ ## Red-Green-Refactor
34
+
35
+ > Source: https://martinfowler.com/bliki/TestDrivenDevelopment.html
36
+
37
+ The core TDD cycle, repeated in small increments:
38
+
39
+ ### 1. Red — Write a Failing Test
40
+
41
+ Write the smallest test that describes the next piece of behavior. The test MUST fail before you write any production code. A test that passes immediately provides no confidence.
42
+
43
+ **Rules:**
44
+ - Write only ONE test at a time
45
+ - The test should compile/parse but fail at the assertion
46
+ - If the test passes immediately, it's either trivial or testing existing behavior
47
+
48
+ ### 2. Green — Make It Pass
49
+
50
+ Write the MINIMUM code to make the failing test pass. Do not add extra logic, handle cases not yet tested, or optimize.
51
+
52
+ **Rules:**
53
+ - Write the simplest code that makes the test pass
54
+ - It's OK to hardcode values initially — the next test will force generalization
55
+ - Do not add code for future tests
56
+ - All existing tests must still pass
57
+
58
+ ### 3. Refactor — Clean Up
59
+
60
+ With all tests green, improve the code structure without changing behavior. Tests give you the safety net.
61
+
62
+ **Rules:**
63
+ - No new functionality during refactoring
64
+ - All tests must remain green after each refactoring step
65
+ - Remove duplication, improve naming, extract methods
66
+ - Refactor both production code AND test code
67
+
68
+ ### Cycle Length
69
+
70
+ Each Red-Green-Refactor cycle should take 1–10 minutes. If you're spending more than 10 minutes in the Red or Green phase, the step is too large — break it down.
71
+
72
+ ## Transformation Priority Premise
73
+
74
+ > Source: http://blog.cleancoder.com/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html
75
+
76
+ When going from Red to Green, prefer simpler transformations over complex ones. Listed from simplest to most complex:
77
+
78
+ 1. **Constant** — return a hardcoded value
79
+ 2. **Scalar** — replace constant with a variable
80
+ 3. **Direct** — replace unconditional with conditional (if/else)
81
+ 4. **Collection** — operate on a collection instead of a scalar
82
+ 5. **Iteration** — add a loop
83
+ 6. **Recursion** — add recursive call
84
+ 7. **Assignment** — replace computed value with mutation
85
+
86
+ **Example — building FizzBuzz with TDD:**
87
+
88
+ ```
89
+ Test 1: input 1 → "1" Transformation: Constant
90
+ Test 2: input 2 → "2" Transformation: Scalar (use the input)
91
+ Test 3: input 3 → "Fizz" Transformation: Direct (add if)
92
+ Test 4: input 5 → "Buzz" Transformation: Direct (add another if)
93
+ Test 5: input 15 → "FizzBuzz" Transformation: Direct (add combined if)
94
+ Test 6: input 1-15 → full list Transformation: Iteration (generalize)
95
+ ```
96
+
97
+ By following this priority, you avoid over-engineering early and let the design emerge naturally from the tests.
98
+
99
+ ## F.I.R.S.T. Principles
100
+
101
+ Every unit test must satisfy these five properties:
102
+
103
+ | Principle | Definition | Violation Signal |
104
+ |-----------|------------|-----------------|
105
+ | **Fast** | Runs in milliseconds | Real I/O, network calls, `sleep()` |
106
+ | **Independent** | No dependency on other tests | Shared mutable state, ordered execution |
107
+ | **Repeatable** | Same result every run | System clock, random data without seed, race conditions |
108
+ | **Self-Validating** | Pass or fail without manual interpretation | Tests that print output for a human to read |
109
+ | **Timely** | Written before or alongside production code | Tests added weeks after a feature shipped |
110
+
111
+ F.I.R.S.T. is a diagnostic checklist: if a test violates any property, it will erode team trust and reduce the value of the suite.
112
+
113
+ ## London School vs Detroit School
114
+
115
+ > Source: https://martinfowler.com/articles/mocksArentStubs.html
116
+
117
+ Two schools of TDD with different philosophies on test doubles. Most teams use a hybrid.
118
+
119
+ ### Detroit School (Classicist, Inside-Out)
120
+
121
+ - **Unit definition**: A module of any size — can span multiple classes
122
+ - **Approach**: Bottom-up; start from domain logic, build outward
123
+ - **Test doubles**: Avoid mocks; use real objects when feasible
124
+ - **Verification**: State verification — examine the result after execution
125
+ - **Testing style**: Black-box; test through public API
126
+ - **Refactoring**: Safe — tests aren't coupled to implementation details
127
+ - **Best for**: Building confidence in real interactions; reducing brittleness
128
+
129
+ ### London School (Mockist, Outside-In)
130
+
131
+ - **Unit definition**: A single class in isolation
132
+ - **Approach**: Top-down; start from the API, work inward
133
+ - **Test doubles**: Mock all collaborators
134
+ - **Verification**: Behavior verification — confirm correct method calls occurred
135
+ - **Testing style**: White-box; tests know about internals
136
+ - **Refactoring**: Can be brittle — tests break when implementation changes
137
+ - **Best for**: Designing interactions upfront; driving architecture decisions
138
+
139
+ ### Recommended: Hybrid Approach
140
+
141
+ Apply Detroit discipline as the default — use real objects, verify state. Apply London mocking only at architectural boundaries (external APIs, databases, clocks). Never mock value objects, pure functions, or in-process helpers.
142
+
143
+ The most important rule: if you're mocking to make a test easy to write, that's often a design smell (see The Hard Test in anti-patterns). If you're mocking because the dependency is genuinely external or slow, that's the right use.
144
+
145
+ ## When to Use TDD
146
+
147
+ TDD is most valuable when:
148
+
149
+ - **Business logic** — Complex rules, calculations, state machines. TDD forces you to think through all cases before implementing.
150
+ - **Algorithm development** — Sorting, parsing, validation, transformation logic. Tests serve as a specification.
151
+ - **Bug fixes** — Write a test that reproduces the bug first (Red), then fix it (Green). This prevents regressions.
152
+ - **API/interface design** — Writing tests first helps you design interfaces from the consumer's perspective.
153
+ - **Refactoring** — Ensure tests exist before refactoring. If they don't, write characterization tests first, then refactor.
154
+
155
+ ## When TDD Is Less Effective
156
+
157
+ TDD is not universally optimal. Use judgment:
158
+
159
+ - **UI/visual components** — Layout, styling, animations are hard to express as unit tests. Use visual regression testing or snapshot tests instead.
160
+ - **Exploratory/prototype code** — When you don't know what to build yet, writing tests first slows exploration. Spike first, then write tests.
161
+ - **Thin integration layers** — Simple pass-through code (e.g., a controller that calls a service) may not benefit from test-first approach. Integration tests are more valuable here.
162
+ - **Infrastructure/glue code** — Database migrations, config files, build scripts. Test these with integration or end-to-end tests.
163
+ - **External API wrappers** — Thin clients wrapping external APIs are better tested with integration tests against the real (or sandboxed) API.
164
+
165
+ For these cases, write tests AFTER the implementation (test-last), but still write them.
166
+
167
+ ## BDD and ATDD Extensions
168
+
169
+ ### Behavior-Driven Development (BDD)
170
+
171
+ > Source: https://martinfowler.com/bliki/GivenWhenThen.html
172
+
173
+ BDD extends TDD by using natural language to describe behavior. Useful when tests need to be readable by non-developers.
174
+
175
+ **Given-When-Then** structure:
176
+
177
+ ```gherkin
178
+ Given a cart with items totaling $100
179
+ When a 10% discount is applied
180
+ Then the total should be $90
181
+ ```
182
+
183
+ Maps to test code:
184
+
185
+ ```python
186
+ def test_cart_with_10_percent_discount_totals_90():
187
+ # Given
188
+ cart = Cart(items=[Item(price=100)])
189
+
190
+ # When
191
+ cart.apply_discount(PercentageDiscount(10))
192
+
193
+ # Then
194
+ assert cart.total == 90.0
195
+ ```
196
+
197
+ ### Acceptance TDD (ATDD)
198
+
199
+ Write high-level acceptance tests before implementing a feature. These tests describe the feature from the user's perspective and drive the overall design. Unit tests (via TDD) then drive the implementation of each component.
200
+
201
+ **Flow:**
202
+ 1. Write acceptance test (fails — Red)
203
+ 2. Use TDD to implement components needed to pass it
204
+ 3. Acceptance test passes (Green)
205
+ 4. Refactor
206
+
207
+ ATDD is most valuable for features with clear acceptance criteria and when working with product owners or stakeholders.
208
+
209
+ ## Advanced Techniques
210
+
211
+ ### Property-Based Testing
212
+
213
+ Instead of writing individual input/output pairs, define **properties** that should always hold true and let a framework generate hundreds of test cases automatically.
214
+
215
+ **Best for:** Pure functions, algorithms, data transformations, serialization round-trips.
216
+
217
+ **Tools:**
218
+ - Python: [Hypothesis](https://hypothesis.readthedocs.io)
219
+ - JavaScript/TypeScript: [fast-check](https://fast-check.dev)
220
+ - Go: `testing/quick` (stdlib), [gopter](https://github.com/leanovate/gopter)
221
+ - Rust: [proptest](https://github.com/proptest-rs/proptest)
222
+ - Java: [jqwik](https://jqwik.net)
223
+ - Elixir: [StreamData](https://hexdocs.pm/stream_data)
224
+
225
+ **Example property** (Python/Hypothesis):
226
+ ```python
227
+ from hypothesis import given, strategies as st
228
+
229
+ @given(st.lists(st.integers()))
230
+ def test_sort_is_idempotent(lst):
231
+ assert sorted(sorted(lst)) == sorted(lst)
232
+ ```
233
+
234
+ ### Mutation Testing
235
+
236
+ Mutation testing introduces small code changes (mutations) and checks whether your tests catch them. A test suite that lets mutations survive has gaps in its coverage.
237
+
238
+ **Metric:** Mutation score = % of mutations killed. Target 80%+.
239
+
240
+ **Tools:**
241
+ - JavaScript/TypeScript/C#: [Stryker](https://stryker-mutator.io)
242
+ - Java: [PITest](https://pitest.org)
243
+ - Python: [mutmut](https://mutmut.readthedocs.io)
244
+ - Go: [go-mutesting](https://github.com/zimmski/go-mutesting)
245
+
246
+ Run mutation testing periodically (not on every commit) to identify weak spots in the test suite.
247
+
248
+ ### Contract Testing
249
+
250
+ In microservice or distributed architectures, contract tests verify that services communicate correctly without running full integration tests.
251
+
252
+ **How it works:**
253
+ 1. Consumer defines a contract (expected interactions)
254
+ 2. Provider verifies it can fulfill the contract
255
+ 3. Both test independently — no need to spin up the full system
256
+
257
+ **Tool:** [Pact](https://pact.io) — supports most major languages.
258
+
259
+ Contract tests replace the expensive integration test layer for inter-service communication while still catching breaking API changes early.
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "python-pack",
3
+ "version": "1.0.0",
4
+ "agents": [],
5
+ "skills": [
6
+ {
7
+ "name": "grimoire.unit-testing-python",
8
+ "path": "skills/grimoire.unit-testing-python",
9
+ "description": "Python unit testing specialist. Provides framework selection, conventions, and patterns for pytest and unittest.",
10
+ "version": "1.0.0",
11
+ "triggers": {
12
+ "keywords": ["pytest", "unittest", "conftest", "parametrize"],
13
+ "file_extensions": [".py"],
14
+ "patterns": ["write.*test", "add.*test", "create.*test", "python.*test", "test.*coverage"],
15
+ "file_paths": ["tests/**", "test/**", "**/test_*.py", "**/*_test.py", "**/conftest.py"]
16
+ }
17
+ }
18
+ ]
19
+ }
@@ -0,0 +1,239 @@
1
+ ---
2
+ name: grimoire.unit-testing-python
3
+ description: "Python unit testing specialist. Framework selection, patterns, and best practices for pytest, unittest, and hypothesis. Use when writing tests for .py files, configuring pytest, or asking about Python testing patterns, fixtures, parametrize, mocking, async testing."
4
+ ---
5
+
6
+ # Python Unit Testing
7
+
8
+ Focused guidance for writing clean, idiomatic unit tests in Python projects.
9
+
10
+ ## Framework Selection
11
+
12
+ ### Detection
13
+
14
+ 1. Check existing test files first — always match what the project uses
15
+ 2. Check for `pytest` in `pyproject.toml` dependencies or `[tool.pytest]` section
16
+ 3. Check for `setup.cfg` or `pytest.ini` configuration
17
+ 4. Check imports in existing tests (`import pytest` vs `import unittest`)
18
+
19
+ ### Decision Table
20
+
21
+ | Condition | Use | Reason |
22
+ |-----------|-----|--------|
23
+ | Project has existing tests | **Match existing** | Consistency is paramount |
24
+ | New project, any size | **pytest** | Industry standard, superior ergonomics |
25
+ | stdlib only requirement | **unittest** | Built-in, no dependencies |
26
+ | User explicitly requests | **Requested** | Respect user preference |
27
+
28
+ ## Naming Conventions
29
+
30
+ Use `test_method_scenario_expected` with snake_case:
31
+
32
+ ```python
33
+ # Pattern: test_method_scenario_expected
34
+ def test_get_user_with_invalid_id_raises_not_found(): ...
35
+ def test_calculate_total_with_discount_applies_percentage(): ...
36
+ def test_parse_config_with_missing_fields_raises_validation_error(): ...
37
+
38
+ # Class grouping (optional)
39
+ class TestOrderService:
40
+ def test_process_order_with_valid_input_returns_success(self): ...
41
+ def test_process_order_with_empty_items_raises_validation_error(self): ...
42
+ ```
43
+
44
+ ## Patterns
45
+
46
+ ### AAA with pytest
47
+
48
+ ```python
49
+ import pytest
50
+ from unittest.mock import Mock, AsyncMock, patch
51
+
52
+ @pytest.fixture
53
+ def mock_repo():
54
+ repo = Mock(spec=OrderRepository)
55
+ repo.save = AsyncMock(return_value=Order(id="123"))
56
+ return repo
57
+
58
+ @pytest.fixture
59
+ def service(mock_repo):
60
+ return OrderService(repository=mock_repo)
61
+
62
+ async def test_process_order_with_valid_order_returns_id(service, mock_repo):
63
+ # Arrange
64
+ order = create_valid_order()
65
+
66
+ # Act
67
+ result = await service.process_order(order)
68
+
69
+ # Assert
70
+ assert result.id == "123"
71
+ mock_repo.save.assert_called_once_with(order)
72
+
73
+ async def test_process_order_with_invalid_order_raises_validation_error(service):
74
+ # Arrange
75
+ order = create_invalid_order()
76
+
77
+ # Act & Assert
78
+ with pytest.raises(ValidationError, match="items cannot be empty"):
79
+ await service.process_order(order)
80
+ ```
81
+
82
+ ### Parametrize
83
+
84
+ ```python
85
+ @pytest.mark.parametrize("discount,expected", [
86
+ (0, 100.0),
87
+ (10, 90.0),
88
+ (50, 50.0),
89
+ ])
90
+ def test_apply_discount_calculates_correctly(discount, expected):
91
+ assert apply_discount(100.0, discount) == expected
92
+
93
+ # With IDs for readable test names
94
+ @pytest.mark.parametrize("input_val,expected", [
95
+ pytest.param("", False, id="empty-string"),
96
+ pytest.param("abc", True, id="non-empty-string"),
97
+ pytest.param(" ", True, id="whitespace-only"),
98
+ ])
99
+ def test_is_non_empty(input_val, expected):
100
+ assert is_non_empty(input_val) == expected
101
+ ```
102
+
103
+ ### Fixtures
104
+
105
+ ```python
106
+ # conftest.py — shared across test files
107
+ @pytest.fixture
108
+ def db_session():
109
+ session = create_test_session()
110
+ yield session
111
+ session.rollback()
112
+
113
+ # Fixture composition
114
+ @pytest.fixture
115
+ def order_service(mock_repo, mock_notifier):
116
+ return OrderService(repo=mock_repo, notifier=mock_notifier)
117
+
118
+ # Factory fixture for multiple instances
119
+ @pytest.fixture
120
+ def make_order():
121
+ def _make(status="pending", **kwargs):
122
+ return Order(status=status, **kwargs)
123
+ return _make
124
+
125
+ def test_cancel_pending_order_succeeds(order_service, make_order):
126
+ order = make_order(status="pending")
127
+ result = order_service.cancel(order)
128
+ assert result.status == "cancelled"
129
+ ```
130
+
131
+ ### Async Testing
132
+
133
+ ```python
134
+ import pytest
135
+
136
+ # pytest-asyncio auto mode (recommended)
137
+ # pyproject.toml: [tool.pytest.ini_options] asyncio_mode = "auto"
138
+
139
+ async def test_fetch_user_returns_data(mock_api):
140
+ user = await fetch_user("123")
141
+ assert user.name == "Alice"
142
+
143
+ async def test_fetch_user_with_bad_id_raises(mock_api):
144
+ with pytest.raises(NotFoundError):
145
+ await fetch_user("bad-id")
146
+ ```
147
+
148
+ ### Error Testing
149
+
150
+ ```python
151
+ # Basic exception check
152
+ def test_divide_by_zero_raises():
153
+ with pytest.raises(ZeroDivisionError):
154
+ divide(1, 0)
155
+
156
+ # Match exception message
157
+ def test_invalid_email_raises_with_message():
158
+ with pytest.raises(ValidationError, match="invalid email format"):
159
+ validate_email("not-an-email")
160
+
161
+ # Check exception attributes
162
+ def test_validation_error_includes_field():
163
+ with pytest.raises(ValidationError) as exc_info:
164
+ validate_order(invalid_order)
165
+ assert exc_info.value.field == "items"
166
+ ```
167
+
168
+ ## Mocking
169
+
170
+ ### unittest.mock (stdlib)
171
+
172
+ ```python
173
+ from unittest.mock import Mock, AsyncMock, patch, MagicMock
174
+
175
+ # Basic mock
176
+ mock_repo = Mock(spec=OrderRepository)
177
+ mock_repo.save.return_value = Order(id="123")
178
+
179
+ # Async mock
180
+ mock_repo.save_async = AsyncMock(return_value=Order(id="123"))
181
+
182
+ # Patch decorator
183
+ @patch("myapp.services.email_client")
184
+ def test_sends_email(mock_client):
185
+ mock_client.send.return_value = True
186
+ # ...
187
+
188
+ # Context manager patch
189
+ def test_sends_email():
190
+ with patch("myapp.services.email_client") as mock_client:
191
+ mock_client.send.return_value = True
192
+ # ...
193
+
194
+ # Verify calls
195
+ mock_repo.save.assert_called_once_with(order)
196
+ mock_repo.save.assert_not_called()
197
+ assert mock_repo.save.call_count == 2
198
+ ```
199
+
200
+ ### What NOT to mock
201
+
202
+ - Value objects, dataclasses, named tuples
203
+ - Pure functions with no side effects
204
+ - The class/module under test itself
205
+ - Simple utility functions
206
+
207
+ Mock only at system boundaries: APIs, databases, file system, time, randomness.
208
+
209
+ ## File Conventions
210
+
211
+ - `test_*.py` or `*_test.py` in `tests/` directory
212
+ - `conftest.py` for shared fixtures (per-directory)
213
+ - `pytest.ini`, `pyproject.toml [tool.pytest]`, or `setup.cfg [tool:pytest]` for config
214
+ - Run: `pytest` or `python -m pytest`
215
+
216
+ ## Package Setup
217
+
218
+ ```bash
219
+ # pytest (recommended)
220
+ pip install pytest pytest-asyncio pytest-cov
221
+
222
+ # Add to pyproject.toml
223
+ [tool.pytest.ini_options]
224
+ asyncio_mode = "auto"
225
+ testpaths = ["tests"]
226
+ ```
227
+
228
+ ## Authoritative Sources
229
+
230
+ - pytest: https://docs.pytest.org
231
+ - unittest: https://docs.python.org/3/library/unittest.html
232
+ - Hypothesis (property-based): https://hypothesis.readthedocs.io
233
+ - Kent Beck — Canon TDD: https://tidyfirst.substack.com/p/canon-tdd
234
+ - Martin Fowler — Mocks Aren't Stubs: https://martinfowler.com/articles/mocksArentStubs.html
235
+
236
+ ## Reference Materials
237
+
238
+ - **[Anti-Patterns](reference/anti-patterns.md)** — Common testing mistakes and how to fix them
239
+ - **[TDD Workflow Patterns](reference/tdd-workflow-patterns.md)** — Red-Green-Refactor, Transformation Priority Premise, when to use TDD