agentleFS
Khuirul-HudaKhuirul-Huda/clean-principles/skills/testing-principles/SKILL.md

testing-principles

Skill0 starsChanged 29 days agoMITView on GitHub

Use when writing tests, deciding what to test, or when tests feel brittle, slow, or hard to maintain.

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 5 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every test has three clear sections:

    line 58
  2. Distinctive:

    Rule: If you need to read the test body to understand what it tests, the name is wrong.

    line 100
  3. Distinctive:

    Red: Write a failing test for the behavior you want

    line 130
  4. Distinctive:

    Green: Write the minimum code to make it pass

    line 131
  5. Distinctive:

    Rule: Use coverage to find *untested code* — not to prove correctness.

    line 177

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Testing Principles

Principles for writing tests that give confidence without becoming a maintenance burden.

The Testing Pyramid

        ▲  E2E / Integration   (few, slow, high confidence)
       ▲▲▲  Integration Tests  (some, moderate speed)
      ▲▲▲▲▲  Unit Tests        (many, fast, isolated)

Invert the pyramid = pain: Lots of E2E tests → slow CI, flaky builds, hard to debug.


F.I.R.S.T. Properties of Good Tests

PropertyRule
FastUnit tests in ms. Full suite in < 10 min.
IndependentNo test depends on another's state. Any order = same result.
RepeatableSame result every time. No flakiness.
Self-validatingPass or fail — no manual inspection.
TimelyWritten alongside (or before) production code.

What to Test

✅ Test:                          ❌ Don't test:
  Business logic                   Private methods (test via public API)
  Edge cases & boundaries          Framework internals
  Error paths                      Getters/setters with no logic
  Integration between layers       Implementation details (how, not what)
  Public API contracts             Third-party library behavior

Test behavior, not implementation:

# ❌ BAD: Tests implementation (breaks if you rename _calculate_discount)
assert order._calculate_discount(100) == 10

# ✅ GOOD: Tests behavior (survives refactoring)
assert order.total_with_discount() == 90

Arrange-Act-Assert (AAA)

Every test has three clear sections:

def test_order_applies_discount_for_premium_users():
    # Arrange — set up state
    user = User(tier="premium")
    order = Order(user=user, items=[Item(price=100)])

    # Act — do the thing
    total = order.calculate_total()

    # Assert — verify outcome
    assert total == 90  # 10% premium discount applied

One assertion concept per test — multiple asserts OK if they all verify the same behavior.


Test Naming Convention

A test name should describe: what is tested, under what condition, and the expected outcome.

❌ Bad name✅ Good name
test1test_order_total_includes_tax_for_premium_users
testDiscounttest_discount_not_applied_when_cart_is_empty
test_user()test_user_registration_fails_when_email_already_exists
handleErrortest_payment_raises_exception_when_card_is_declined

Two popular patterns:

# Pattern 1: test_<unit>_<scenario>_<expected> (Python / snake_case)
def test_order_total_returns_zero_when_cart_is_empty(): ...
def test_user_login_raises_error_when_password_is_wrong(): ...
// Pattern 2: BDD style — works great with it() in Jest/Vitest
it('should apply 10% discount when user tier is premium')
it('should reject checkout when cart has no items')

Rule: If you need to read the test body to understand what it tests, the name is wrong.


Test Doubles

DoubleUse when
StubReplace dependency that returns values (no verification)
MockVerify a call was made (with specific args)
FakeLightweight working implementation (in-memory DB)
SpyReal object that records calls
# Stub: control the return value
user_repo = Mock()
user_repo.find.return_value = User(id=1, name="Alice")

# Mock: verify interaction happened
email_service = Mock()
order_service.place_order(order)
email_service.send_confirmation.assert_called_once_with(order)

Prefer fakes over mocks for complex collaborators — they're less brittle.


TDD — Red → Green → Refactor

  1. Red: Write a failing test for the behavior you want
  2. Green: Write the minimum code to make it pass
  3. Refactor: Clean up, keeping tests green
❌ Write code → Write tests   (tests document what you wrote, not what you want)
✅ Write test → Write code    (tests drive the design)

TDD benefit: Forces small, focused functions. Hard-to-test code = bad design signal.


Anti-Patterns

Anti-patternProblemFix
Test the moonTesting too much in one testSplit into focused tests
Brittle testsBreak on every refactorTest behavior, not internals
Mystery guestTest state set in fixture fileArrange state in the test
Slow testsFull DB/network in unit testsUse fakes/stubs
Flaky testsTiming/order-dependentMake truly independent
Test that always passesNo real assertionAssert the actual value

Code Coverage

Coverage is a useful signal, not a goal. 100% coverage ≠ 100% correct.

GuidelineDetail
Reasonable baseline80% line coverage for most projects
Prioritize forBusiness logic, error paths, edge cases
Don't chase forFramework glue, trivial getters, generated code

What coverage cannot catch:

# 100% coverage — but test asserts the wrong thing
def test_discount():
    order = Order(items=[Item(price=100)], user=premium_user)
    total = order.calculate_total()
    assert total == 100  # ❌ Should be 90 — bug undetected, coverage reports 100%
  • A test that runs code but asserts nothing gives 100% coverage and zero confidence
  • Missing test cases (untested scenarios) are invisible to coverage reports

Rule: Use coverage to find untested code — not to prove correctness.


Quick Self-Check

  • Does each test have one clear reason to fail?
  • Can tests run in any order?
  • Do tests verify behavior, not implementation?
  • Can you understand what each test proves from its name alone?
  • Are slow tests isolated from fast tests?
  • Does each test name describe the scenario and expected outcome without reading the body?
  • Is coverage tracking business logic rather than chasing a 100% number?

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.