Skip to content
AI Skill Index
CodingClaudeChatGPT

Write tests for this function

Table-driven tests covering the boundaries, not just the happy path.

Author: EditorialWritten for: ChatGPT, Claude
Sign in to save
Write tests for the function below using [TEST FRAMEWORK].

Cover:
- The ordinary case.
- Every boundary in the input domain (empty, one, many, maximum).
- Each error path, asserting the error, not just that it threw.

Prefer one table-driven test over many near-identical ones. Do not test the language.

```
[PASTE FUNCTION HERE]
```

Highlighted parts are placeholders. Replace them before you send.

Worked example

The input is invented for this page. The output shows what the prompt asks for with that input.

Example input (invented)

Test framework: pytest

Function

def chunk(items, size):
    """Split a list into consecutive lists of at most `size` items."""
    if size < 1:
        raise ValueError("size must be at least 1")
    return [items[i:i + size] for i in range(0, len(items), size)]

Example output

import pytest

from chunking import chunk


@pytest.mark.parametrize(
    "items, size, expected",
    [
        pytest.param([1, 2, 3, 4, 5], 2, [[1, 2], [3, 4], [5]], id="uneven tail"),
        pytest.param([1, 2, 3, 4], 2, [[1, 2], [3, 4]], id="exact multiple"),
        pytest.param([], 3, [], id="empty list"),
        pytest.param([1], 3, [[1]], id="one item"),
        pytest.param([1, 2, 3], 1, [[1], [2], [3]], id="smallest size"),
        pytest.param([1, 2, 3], 3, [[1, 2, 3]], id="size equals length"),
        pytest.param([1, 2, 3], 10, [[1, 2, 3]], id="size above length"),
    ],
)
def test_chunk_splits_in_order(items, size, expected):
    assert chunk(items, size) == expected


@pytest.mark.parametrize("size", [0, -1])
def test_chunk_rejects_size_below_one(size):
    with pytest.raises(ValueError, match="size must be at least 1"):
        chunk([1, 2, 3], size)


def test_chunk_leaves_the_input_unchanged():
    items = [1, 2, 3]
    chunk(items, 2)
    assert items == [1, 2, 3]

The import assumes the function lives in chunking.py. The table covers the ordinary case and the boundaries: an empty list, one item, the smallest size, and a size equal to and above the length. The error test asserts the message, so a different ValueError would not pass it. There is no test for a size that is not a whole number, because the function does not say what should happen then.

ShareXLinkedIn

3 of the 7 other prompts filed under Coding.

Coding tools and skills

3 of the 9 tools and 3 of the 7 skills filed under Coding, the same category as this prompt.

Tool

Aider

Pair programming with an LLM in the terminal, wired directly to git.

Open source
Tool

Claude Code

Anthropic's agentic coding tool for the terminal and IDE.

Paid
Tool

Cline

An open-source coding agent that runs inside VS Code with your own API key.

Open source
Skill

Release notes writer

Writes release notes and a changelog from a commit range, leaving out the commits users never notice.

Claude Code, Any agent
Skill

Test writer

Writes tests that fail for the right reason and refuse to test the framework.

Claude Code, Cursor, Any agent

Guides that use this prompt

2 guides list this prompt under "Used in this tutorial".

Coding

Reviewing code an AI wrote

It compiles, the tests pass, and it reads well. That combination is exactly why it needs a different kind of review.

Intermediate·2 min read

Coding guides

1 of the 6 guides filed under Coding, the same category as this prompt.

Cookies

We use only the cookies this site needs to work. Optional analytics helps us see which content is worth writing more of — nothing loads until you say yes.

Read the privacy policy