Skip to content
Skills Library
Programming · Testing

write-tests

/write-tests

Write or review unit tests for code in this repo. Use when writing new tests, reviewing existing tests, or adding test coverage.

Licence
MIT
Last repository change
24.10.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
1
Deprecation

No signals detected. Repository changed on 24.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Write or review unit tests for code in this repo. Use when writing new tests, reviewing existing tests, or adding test coverage.

Original author description

Write or review unit tests for code in this repo. Use when writing new tests, reviewing existing tests, or adding test coverage.

How it works

  1. Core Principles
  2. File and Function Structure
  3. Float Comparisons
  4. Parametrize Similar Tests
  5. Regression Tests
  6. Fixtures
  7. Documentation
  8. Imports
  9. What NOT to Test
Original SKILL.md headings · 177 lines, 793 words
SKILL.md preview
---
name: write-tests
description: Write or review unit tests for code in this repo. Use when writing new tests, reviewing existing tests, or adding test coverage.
---

# Writing Unit Tests

Follow these conventions when writing or modifying tests in this codebase.
Apply them within the requested changes; do not reformat or reorganize the existing
test suite to satisfy the deferred pytest-style rules. Follow the current docstring
format in [AGENTS.md](../../../AGENTS.md).

## Core Principles

- **Only write tests that provide real information.** Avoid testing trivial things like object construction or obvious attribute access. Test behavior and computations.
- **Don't check types in tests.** Types are verified by the static type checker (`ty`). Avoid using `isinstance`, `type()`, or other type assertions in tests.
- **Write doctests to explain function usage.** Doctests are a quick way to show how to use a function and expected output; they also are quick smoke tests.

## File and Function Structure

Tests live in `tests/` within each package.
Name files `test_<module>.py`.
Use plain functions, never classes:

```python
"""Tests for the Spam."""

import numpy as np
from numpy.testing import assert_almost_equal as aae
from pytest import approx, mark, param, raises

from spam import Spam

def test_classic_spam() -> None:
    """Test classic spam values."""
    classic_spam = Spam("classic")
    assert classic_spam.weight == approx(3.14159265)
```

- Add a module-level docstring to every test file.
- Give each test function a concise one-line docstring.
- Define module-level structure constants (e.g., `water`) for reuse across multiple tests. Don't reload them inside tests.

## Float Comparisons

Use default tolerance thresholds unless there is a specific, documented reason to override them.

| Use case | Tool | Import |
|---|---|---|
| Scalars and simple lists | `approx` | `from pytest import approx` |
| NumPy arrays (gradients, hessians) | `aae` | `from numpy.testing import assert_almost_equal as aae` |
| Exact/near-exact array equality | `assert_allclose` | `from numpy.testing import assert_allclose` |

```python
# Scalars
assert Spam("classic").weight == approx(3.14159265)

# Lists / tuples
assert Spam("jalapeno").scores == approx([1, 2, 3])

# NumPy arrays
aae(Spam("maple").distribution, [[0, 1, 2], [3, 4, 5], [6, 7, 8]])

# Only override thresholds when truly necessary, with a comment explaining why
assert Spam("bacon").weight == approx(3.14, abs=0.005)  # loose: GitHub Actions gives different results

# Exceptions
with raises(ValueError):
    Spam("classic").process(threshold=-1)
```

## Parametrize Similar Tests

Use `pytest.mark.parametrize` to collapse tests that differ only in inputs/outputs
Use `param(..., marks=[mark.regression])` to apply markers to individual parametrized cases

```python
@mark.parametrize(
    ("spam_type", "weight"),
    [
        ("classic", 3.14159265),
        ("maple", 4.1),
        param("low sodium", 2.9, marks=[mark.regression]),
    ],
)
def test_weights(spam_type: str, weight: float) -> None:
    """Test energy for multiple molecules."""
    spam = Spam(spam_type)
    assert spam.weight == approx(weight)
```

## Regression Tests

Mark tests that run less regularly but protect against regressions with `@mark.regression`. All tests currently run by default. The template's `-m 'not regression'` exclusion
is deferred until test classification is reviewed.

When a regression test is related to a GitHub issue, link it in the docstring:

```python
@mark.regression
def test_low_sodium_spam() -> None:
    """They forgot to remove the sodium originally.

    https://example.com/flying_circus/spam/issues/1
    """
    ...
```

Regression tests verify:

- Previously-broken behavior is fixed and stays fixed
- A wider range of code paths (e.g., all solvents, all supported methods)
- Edge cases that are expensive to test on every CI run

## Fixtures

Use pytest fixtures for any setup that is repeated across multiple tests. Place fixtures in `conftest.py`:

- **Within a package:** `tests/conftest.py`
- **Shared across packages:** not currently applicable; each package has its own `conftest.py`

## Documentation

Docstrings should concisely state what is being tested in the initial sentence.
Additional information about why should be placed in the follow-up paragraph.
If available, include the issue number.
Do not explain code in tests, only what is being tested

Comments should be used sparingly and only when targetted information is needed
(e.g. the value a test would be if something weren't working)

### Example

```python
def test_sum_on_negatives() -> None:
    """Sums of negative numbers should be negative.

    #153 found that sums of negative numbers were positive.
    """
    # previously 5
    assert sum(-2, -3) == -5
    assert sum(-1.3, -4.5) == -5.8
```

### Incorrect example (do not do this!)

```python
def test_sum() -> None:  #  ❌ not descriptive enough
    """Check negative sums.  # ❌ not descriptive enough

    Fixes #153  #  ❌ does not describe the problem
    """
    # The sum function adds two numbers  #  ❌ don't add code description as comments
    assert sum(-2, -3) == -5
    assert sum(-1.3, -4.5) == -5.8
```

## Imports

Prefer importing selectively from pytest for readability

```python
from pytest import approx, mark, param, raises
```

Standard alias for numpy array assertions:

```python
from numpy.testing import assert_almost_equal as aae
```

## What NOT to Test

- Type correctness (use `uv run ty check` instead)
- That a function returns something (trivial)
- Exact exception message text (fragile)
- Behavior of external libraries
- Implementation details that should be free to change

Focus on observable behavior: computed values, raised exceptions for invalid inputs, and correct transformations.

Versions

RepositoryScoreLicence
This page · rowansci/rowan-python100MIT
onsi/biloba100MIT
runwield/runwield100MIT
dzhng/jevgrep100MIT
lukedj78/dev-flow100MIT
dzhng/skills100MIT
yaohuangguan/agent-playbook100MIT
microsoft/semantic-link-labs100MIT
qa-aman/claude-skills100MIT

Database snapshot: 13.11.2026 · package version b0c335e6a77d (SHA-256 digest of the entire package)

Downloads in the last 30 days: 3 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100write-code
    Write or review code in this repo. Use when writing new code, reviewing existing code, or editing code.
  • 100v1-write-tests
    Use when writing unit tests for code changes or new functionality. Triggers on "write tests", "add tests", "test this code".
  • 100test-generator
    Generate unit and integration tests for project code. Use when new code is written or test coverage needs improvement.
  • 100create-unit-test
    Write or refactor unit tests following the canonical project pattern. Use when adding new unit tests or standardizing existing ones.
  • 100test-unit
    Write unit and integration tests for a named module or change. Use when "write unit tests" or "add tests for this function". Coverage plan → plan-test-coverage. Mutation score → test-mutation. Browser check → test-playwright.
  • 90Android Test
    Write and run Android unit tests and on-device instrumentation/UI tests. Use when the user asks for tests, coverage, or to verify a screen/flow on the phone.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.