Writing tests

From bellini666/dotfiles · View interactive version

Writing tests is an AI agent skill from the GitHub repository bellini666/dotfiles. This skill should be used when the user asks to 'write tests', 'add tests', 'create test cases', 'cover this with tests', 'unit tests', 'integration tests', 'test coverage', 'test this function', 'fix failing tests', or when implementing tests as part of a feature or bug fix task.

How to use this skill

Skill file

Writing Tests

Test Philosophy

These principles are non-negotiable:

These rules apply regardless of language or framework. The examples below use pytest, but the principles are universal.

Discovery: Before Writing a Single Test

Before writing any test, spend 2 minutes understanding how the codebase already tests things:

  1. Find the test directory — look for tests/, test/, __tests__/, or spec/ directories
  2. Read 2-3 existing test files — note the style (function vs class, fixtures vs setup methods, assertion style)
  3. Check for conftest.py / test helpers — identify reusable fixtures, factories, and utilities
  4. Check the test runner config — look at pyproject.toml, pytest.ini, setup.cfg, or jest.config for custom markers, plugins, and conventions
  5. Match existing patterns — do not invent a new testing style when the project has an established one
# Quick discovery commands
find . -name "conftest.py" -type f       # Find all conftest files
grep -r "def test_" tests/ | head -20    # See naming patterns
grep -r "@pytest.fixture" tests/ | head  # Find existing fixtures

Rules

  1. Reuse test files — add tests to existing files when testing the same module or feature
  2. Use parametrized tests — group cases with same logic but different inputs via parametrize
  3. No obvious comments — code should be self-explanatory
  4. Follow existing style — match patterns in the existing test suite exactly
  5. Imports at module level always — never import inside test functions. The only valid exceptions are: testing import errors, testing lazy import behavior, or when the test semantically depends on the import happening at that point. "Convenience" or "keeping imports close to usage" are not valid reasons.
  6. Reuse fixtures — use existing fixtures; create new ones only when needed
  7. Detect project test style — examine existing tests to determine function vs class style; match it
  8. Place helpers correctly — put shared helpers in existing conftest.py or test helper locations
  9. Watch a regression test fail first — when the test closes a specific defect, run it against the unfixed code and paste the failure. Write it before the fix, or revert the fix and re-run. A regression test only ever seen passing proves nothing. Feature tests are exempt.

Test Naming

Name tests using the pattern: test_<what>_<condition>_<outcome>

The name should read as a sentence describing the behavior being verified:

# Good: descriptive, reads naturally
def test_login_with_valid_credentials_returns_token():
    user = create_user(email="test@example.com", password="secure123")
    response = client.post("/login", json={"email": user.email, "password": "secure123"})
    assert response.status_code == 200
    assert "token" in response.json()

def test_login_with_wrong_password_returns_401():
    user = create_user(email="test@example.com", password="secure123")
    response = client.post("/login", json={"email": user.email, "password": "wrong"})
    assert response.status_code == 401

# Bad: vague, doesn't describe behavior
def test_login():
    ...

def test_login_error():
    ...

Test Structure: Arrange-Act-Assert

Every test follows the AAA pattern. Use blank lines to separate the three sections — do NOT add # Arrange, # Act, # Assert comments. The structure should be self-evident from the whitespace:

def test_order_with_multiple_items_calculates_total():
    order = Order()
    widget = Item("Widget", price=10.00)
    gadget = Item("Gadget", price=25.00)

    order.add_item(widget)
    order.add_item(gadget)

    assert order.total == 35.00

Only add comments when something non-obvious needs explanation (e.g., why a specific value was chosen, why setup is unusual):

def test_rate_limiter_allows_request_after_window_expires():
    limiter = RateLimiter(max_requests=2, window_seconds=60)
    limiter.record("user-1")
    limiter.record("user-1")

    # Advance past the rate limit window
    freeze_time(seconds=61)

    assert limiter.allow("user-1") is True

Each test verifies one specific behavior. When two unrelated assertions are needed, write two tests.

Parametrized Tests

When multiple test cases share the same logic but differ in inputs and expected outputs, use parametrize instead of writing N near-identical test functions:

import pytest

@pytest.mark.parametrize(
    "input_str, expected",
    [
        ("hello world", "Hello World"),
        ("ALREADY CAPS", "Already Caps"),
        ("", ""),
        ("single", "Single"),
        ("multiple   spaces", "Multiple   Spaces"),
    ],
)
def test_title_case_conversion(input_str, expected):
    assert to_title_case(input_str) == expected


@pytest.mark.parametrize(
    "amount, currency, expected_error",
    [
        (-1, "USD", "Amount must be positive"),
        (0, "USD", "Amount must be positive"),
        (100, "INVALID", "Unsupported currency"),
    ],
    ids=["negative-amount", "zero-amount", "invalid-currency"],
)
def test_payment_with_invalid_input_raises(amount, currency, expected_error):
    with pytest.raises(ValidationError, match=expected_error):
        process_payment(amount=amount, currency=currency)

Use the ids parameter when the parametrize values alone don't make test output readable.

Fixtures

Use fixtures to share setup logic. Prefer narrowly-scoped fixtures and compose them:

@pytest.fixture
def active_user(db):
    return UserFactory(is_active=True)


@pytest.fixture
def authenticated_client(client, active_user):
    client.force_authenticate(user=active_user)
    return client


def test_dashboard_returns_user_data(authenticated_client, active_user):
    response = authenticated_client.get("/dashboard/")
    assert response.status_code == 200
    assert response.json()["email"] == active_user.email

Guidelines for fixtures:

Common Pytest Features

Test Coverage Priorities

When adding tests to existing code, prioritize in this order:

  1. Happy path — the primary intended behavior works
  2. Error conditions — invalid input, missing data, permission denied
  3. Edge cases — empty collections, boundary values, unicode, None where unexpected
  4. Integration points — database queries, API calls, file I/O

Do not aim for 100% line coverage at the expense of meaningful tests. One well-designed parametrized test is worth more than five trivial ones.

Red Flags

Stop immediately if about to:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.