Test Writing Skill
Produces tests that fail when the code is wrong — not tests that exist for coverage theatre. Covers happy path, edge cases, error paths, and state transitions. Does NOT chase 100% coverage as a goal; chases meaningful coverage that catches real regressions.
When to Use
- Writing tests for new code or features.
- Adding coverage to an untested area before changing it.
- A bug slipped through that tests should have caught.
- Practicing test-driven development.
- The
verify-before-done skill flagged missing coverage.
- NOT when the user explicitly asked for a spike or throwaway script.
Prerequisites
- A test framework installed for the stack (pytest, vitest, jest, go test, xUnit, JUnit).
- Ability to run the test suite via the
powershell tool.
- The
view, edit, and grep tools to read existing patterns.
- A baseline run of the existing suite passing.
How to Run
1. Read existing tests with `view`/`grep` to copy the project's pattern.
2. List scenarios: happy path, edges, errors, state transitions.
3. Write one test per scenario using Arrange-Act-Assert.
4. Run the suite — confirm new tests pass and nothing else broke.
5. If TDD: write the test first, watch it fail for the right reason, then implement.
Quick Reference
| Stack |
Framework |
Run command |
| TypeScript |
Vitest / Jest |
npm test or npx vitest |
| Python |
pytest |
pytest or python -m pytest |
| Java |
JUnit 5 |
mvn test or gradle test |
| Go |
testing |
go test ./... |
| .NET |
xUnit |
dotnet test |
| Scenario type |
Must cover |
| Happy path |
Valid input → expected output |
| Edge case |
Empty, null, boundary, maximum size |
| Error case |
Invalid input, dependency failure, timeout |
| State transition |
Stateful object moves between valid states |
Procedure
Step 1: Read the Existing Pattern
Use grep for test_* or *.spec.* files and view one of them. Match the project's naming, location, fixtures, and assertion style. Do not introduce a second testing dialect.
Step 2: Enumerate Scenarios
For the code under test, list:
- Happy path inputs.
- Edge cases (empty, null, boundary, max).
- Error cases (invalid input, dependency failure).
- State transitions, if stateful.
Step 3: Write Arrange-Act-Assert
def test_calculate_discount_for_premium_user():
# Arrange
user = create_user(tier="premium")
order = create_order(total=100.00)
# Act
discount = calculate_discount(user, order)
# Assert
assert discount == 20.00
Name tests as documentation: test_login_with_expired_token_returns_401, not test_login.
Step 4: For Bug Fixes — Test First
- Write a test that reproduces the bug.
- Run it. Confirm it fails for the right reason.
- Implement the fix.
- Run it. Confirm it passes.
- Run the full suite — no regressions.
Step 5: Run and Record
Run the suite via the powershell tool. Capture pass/fail counts and paste them into the tasks/todo.md Verification Results block (per verify-before-done).
Pitfalls
- DO NOT write tests that cannot fail (
assert user is not None on a freshly constructed object).
- DO NOT test implementation details (e.g. mocking that
quicksort was called). Test the behavior.
- DO NOT mock the world. Prefer real collaborators where the cost is acceptable.
- DO NOT ignore a flaky test — fix it or delete it. A flaky test erodes trust in the whole suite.
- DO NOT chase 100% coverage as a target. Coverage is a metric, not a mission.
Verification
1---
2name: test-writing
3description: Writes meaningful tests that actually catch bugs.
4tier: practical
5category: workflow
6created_by: human
7platforms: [windows, macos, linux]
8tags: [testing, tdd, quality]
9author: Andreas Wasita (@andreaswasita)
10metadata:
11 source: https://skillmd.com/skills/andreaswasita/test-writing
12---
13
14# Test Writing Skill
15
16Produces tests that fail when the code is wrong — not tests that exist for coverage theatre. Covers happy path, edge cases, error paths, and state transitions. Does NOT chase 100% coverage as a goal; chases meaningful coverage that catches real regressions.
17
18## When to Use
19
20- Writing tests for new code or features.
21- Adding coverage to an untested area before changing it.
22- A bug slipped through that tests should have caught.
23- Practicing test-driven development.
24- The `verify-before-done` skill flagged missing coverage.
25- NOT when the user explicitly asked for a spike or throwaway script.
26
27## Prerequisites
28
29- A test framework installed for the stack (pytest, vitest, jest, go test, xUnit, JUnit).
30- Ability to run the test suite via the `powershell` tool.
31- The `view`, `edit`, and `grep` tools to read existing patterns.
32- A baseline run of the existing suite passing.
33
34## How to Run
35
36```text
371. Read existing tests with `view`/`grep` to copy the project's pattern.
382. List scenarios: happy path, edges, errors, state transitions.
393. Write one test per scenario using Arrange-Act-Assert.
404. Run the suite — confirm new tests pass and nothing else broke.
415. If TDD: write the test first, watch it fail for the right reason, then implement.
42```
43
44## Quick Reference
45
46| Stack | Framework | Run command |
47|---|---|---|
48| TypeScript | Vitest / Jest | `npm test` or `npx vitest` |
49| Python | pytest | `pytest` or `python -m pytest` |
50| Java | JUnit 5 | `mvn test` or `gradle test` |
51| Go | testing | `go test ./...` |
52| .NET | xUnit | `dotnet test` |
53
54| Scenario type | Must cover |
55|---|---|
56| Happy path | Valid input → expected output |
57| Edge case | Empty, null, boundary, maximum size |
58| Error case | Invalid input, dependency failure, timeout |
59| State transition | Stateful object moves between valid states |
60
61## Procedure
62
63### Step 1: Read the Existing Pattern
64
65Use `grep` for `test_*` or `*.spec.*` files and `view` one of them. Match the project's naming, location, fixtures, and assertion style. Do not introduce a second testing dialect.
66
67### Step 2: Enumerate Scenarios
68
69For the code under test, list:
70
711. Happy path inputs.
722. Edge cases (empty, null, boundary, max).
733. Error cases (invalid input, dependency failure).
744. State transitions, if stateful.
75
76### Step 3: Write Arrange-Act-Assert
77
78```python
79def test_calculate_discount_for_premium_user():
80 # Arrange
81 user = create_user(tier="premium")
82 order = create_order(total=100.00)
83
84 # Act
85 discount = calculate_discount(user, order)
86
87 # Assert
88 assert discount == 20.00
89```
90
91Name tests as documentation: `test_login_with_expired_token_returns_401`, not `test_login`.
92
93### Step 4: For Bug Fixes — Test First
94
951. Write a test that reproduces the bug.
962. Run it. Confirm it fails for the right reason.
973. Implement the fix.
984. Run it. Confirm it passes.
995. Run the full suite — no regressions.
100
101### Step 5: Run and Record
102
103Run the suite via the `powershell` tool. Capture pass/fail counts and paste them into the `tasks/todo.md` Verification Results block (per `verify-before-done`).
104
105## Pitfalls
106
107- **DO NOT** write tests that cannot fail (`assert user is not None` on a freshly constructed object).
108- **DO NOT** test implementation details (e.g. mocking that `quicksort` was called). Test the behavior.
109- **DO NOT** mock the world. Prefer real collaborators where the cost is acceptable.
110- **DO NOT** ignore a flaky test — fix it or delete it. A flaky test erodes trust in the whole suite.
111- **DO NOT** chase 100% coverage as a target. Coverage is a metric, not a mission.
112
113## Verification
114
115- [ ] Every new test was observed to fail before it passed (TDD) OR a deliberate mutation was introduced to confirm it fails.
116- [ ] The full suite is green after the change.
117- [ ] Test names describe scenario + expected behavior.
118- [ ] Each bug fix has a regression test that would have caught the original bug.