← IndexEntry № 177·coding

Generate table-driven unit tests for a function with edge cases

Produces a table-driven unit test suite covering happy paths, boundaries, and error conditions for a given function.

Optimized for
ChatGPTClaude
§ When to use this

Writing the first batch of tests for a function is tedious, and it's exactly where people skip the boring-but-important cases: empty inputs, boundaries, error paths. This prompt produces a table-driven suite—one parameterized structure with many cases—so the tests stay readable and easy to extend. It deliberately tells the model to test observable behavior, not private internals, which keeps the tests from breaking every time you refactor. By asking for a one-line rationale per case and a list of cases it could not test, you get insight into the function's testability, not just code. Give it the framework you actually use (pytest, Jest, JUnit, Go's testing package) so the syntax matches your project, and name any external dependencies that need mocking. Treat the result as a strong starting suite: review the expected values, since the model infers them from the code and can inherit a bug if the code itself is wrong. It pairs naturally with the refactoring prompt as your before-and-after safety net.

§ The Prompt— fill in the fields, then copy or open in a tool
§ Customize0/4 fields filled
your prompt — fill the fields above
You are an expert test engineer fluent in [LANGUAGE] and the [TEST_FRAMEWORK] framework. Write a complete, table-driven test suite for the function below. Cover the happy path, boundary values, empty and null inputs, and error conditions. Use descriptive test names that read as specifications. Each case should have a clear input, expected output, and a one-line rationale. Do not test private implementation details—test observable behavior only. Mock these external dependencies if present: [DEPENDENCIES]. Aim for meaningful coverage, not 100% line coverage for its own sake. Here is the function:

[FUNCTION_CODE]

Return runnable test code plus a short list of any cases you could not test and why.
Open with your prompt →ChatGPTClaudeSends your filled-in prompt straight into a new chat.
§ Example Output

What you can expect back

```python
import pytest
from slugs import slugify

@pytest.mark.parametrize('text, expected', [
    ('Hello World', 'hello-world'),      # happy path
    ('  spaced  out  ', 'spaced-out'),   # collapses repeated whitespace
    ('UPPER', 'upper'),                  # lowercases
    ('single', 'single'),                # no separators needed
])
def test_slugify_returns_expected(text, expected):
    assert slugify(text) == expected

@pytest.mark.parametrize('bad', ['', None])
def test_slugify_rejects_empty(bad):
    with pytest.raises(ValueError):
        slugify(bad)
```

Cases I could not fully test:
- Unicode/accented input: behavior is undefined in the code—confirm intended output before asserting.
- None is passed to `.lower()` only after the empty check; verify None is meant to raise ValueError, not TypeError.

Illustrative example — your results will vary by tool and inputs.

§ Pro Tips

Get sharper results

  • 01Read every expected value—the model derives them from the code, so a bug in the code becomes a bug in the test.
  • 02Name your framework precisely (e.g. 'pytest with parametrize') to avoid getting unittest-style boilerplate.
  • 03Ask for property-based tests as a follow-up for functions with large input spaces.
  • 04If a case is marked untestable, that's often a hint the function needs a clearer contract.
§ Variations

Adapt it for your case

Property-based tests

Add 'Also include Hypothesis (or fast-check) property tests that assert invariants across random inputs.'

Snapshot/regression

Add 'For complex object outputs, generate a snapshot test capturing the current output as the baseline.'

Tags#testing#unit-tests#tdd
§ FAQ

Common questions

Will these tests catch bugs in my function?

Only if your expected values are correct. The model infers them from the code, so review them—otherwise the tests just lock in current (possibly wrong) behavior.

Why table-driven instead of separate test functions?

Table-driven tests keep many cases readable and make adding a new case a one-line change, which encourages broader coverage.

Can it mock my database or API?

Yes—list those under [DEPENDENCIES] and the prompt will stub them so tests stay fast and deterministic.

§ Related Entries

You may also need