# Test-first or test-after: what actually reduces bugs?

By Vishal Kumar · Published 2026-09-20 · Updated 2026-11-13
Source: https://fungyaan.com/p/test-first-or-test-after
Tags: how-we-build, testing, tdd, quality

**Short answer:** the verdict, as the cases below will show: the order matters less than *what* you test and *against what*.
- **Test-first (TDD)** works best for pure logic with clear expected behaviour, and for every bug fix: write the failing test that reproduces the bug, then fix it.
- **Test-after** is fine when you're exploring a design, as long as the tests are written before the code is merged, not "later".
- **Property-based tests** find edge cases you didn't think of.
- **Integration tests against real dependencies** (a real database, a real queue in Docker) catch the bugs that matter most in services: queries, permissions, transactions, concurrency.

The case is familiar: a suite that's green, a release that ships, and a bug in production anyway. Was it because the tests were written after the code instead of before? That's the usual suspect, and it's mostly a red herring. People argue about this with real passion, because every approach here contains a good idea. Kent Beck's insight with TDD was that a test can shape a design as well as check it; QuickCheck's authors, Koen Claessen and John Hughes, showed that a computer can invent better edge cases than we can. The fun is in using each where it shines.

> **Case file**
>
> | | |
> |---|---|
> | **The case** | A green suite, a release, and a bug in production anyway |
> | **Suspects** | Tests written after the code instead of before |
> | **Culprit** | Testing the wrong thing, or against mocks instead of real dependencies |
> | **The fix** | Match the test to the bug: TDD for logic, properties for edge cases, integration tests against the real database |
> | **Evidence** | The blogging platform's store tests against a real PostgreSQL, and the database trade-offs lab |

## The suspects at a glance

| | TDD | Test-after | Property-based | Integration-heavy |
|---|---|---|---|---|
| **You write** | A failing test, then the code | Code, then tests | Rules that must always hold; the tool generates inputs | Tests that run the real stack (database, queue) |
| **Best for** | Parsers, rules, calculations, bug fixes | Exploratory code, UI, prototypes | Parsers, encoders, data structures, anything with invariants | Services, data access, security rules, concurrency |
| **Catches** | Logic errors; also shapes the API | Regressions | Edge cases (empty, huge, unicode, boundaries) | Wrong SQL, missing permissions, race conditions, config |
| **Cost** | Discipline; slower start | Risk of skipping or writing weak tests | Learning to state properties | Slower tests; need Docker in CI |

The "Catches" row is the clue. Each approach catches a different kind of bug, so the question was never which one wins. It's which kind of bug you're hunting.

```mermaid
flowchart TD
  A["What kind of bug are you hunting?"] --> B{"A bug fix?"}
  B -- yes --> T1["Failing test first, then the fix"]
  B -- no --> C{"Pure logic with clear expected output?"}
  C -- yes --> T2["TDD with a table of cases, plus properties or fuzzing"]
  C -- no --> D{"SQL, permissions, transactions or concurrency?"}
  D -- yes --> T3["Integration tests against the real database"]
  D -- no --> T4["Explore, then test before merging"]
```

## Case 1: a parser

**The situation:** you need to normalise messy part numbers (`AB-123/x`, `ab123x`, `AB 123 X`) to one form.

**What I'd do:** TDD with a table of cases, then a property test.
- Write the table first: input, expected output. Each new oddity from real documents becomes a row.
- Add properties, which is where it gets interesting: normalising twice gives the same result as once (*idempotence*); the output contains only allowed characters; no input crashes it. You stop listing suspects one by one and describe what every innocent answer must look like.

Go's built-in fuzzing (`go test -fuzz`) is a cheap way to get the "no input crashes it" property. The [part-number matching post](/p/match-messy-part-numbers) has the real cases.

## Case 2: a security rule in the database

**The situation:** each blog's posts must be invisible to every other tenant, enforced by PostgreSQL row-level security.

**What I'd do:** integration tests against a real PostgreSQL, written alongside the policy.
- Create two tenants. As tenant A, try to read, update and delete tenant B's rows. Every attempt must fail or return nothing.
- Try with no tenant set: nothing must match.
- Run as the same database role the app uses in production, not a superuser (superusers, and table owners unless RLS is forced, bypass it, so such a test proves nothing).

The blogging platform I run has store tests like these. Here's the false lead: a mock of the database would pass all of them and prove nothing. There's something reassuring about watching a real database refuse tenant A's request for tenant B's rows. The [RLS post](/p/postgres-rls-tenant-isolation) explains the policies.

## Case 3: a concurrency bug

**The situation:** two requests occasionally sell the last item twice. The bug only shows up sometimes, which is what makes it a proper mystery.

**What I'd do:** reproduce it with a test before fixing it.
- Write a test that starts many concurrent requests against a real database and checks the invariant (stock never below zero, exactly one winner).
- Watch it fail. That failing test is the confession. Fix it (a conditional update, a lock, or optimistic concurrency).
- Keep the test; run it with Go's race detector where relevant.

The [lock or retry post](/p/lock-or-retry) and the [database trade-offs lab](https://github.com/the-vishal-kumar/database-tradeoffs-lab) have tests in this style: each one pins a behaviour (who wins, what's blocked, what's lost) so a change in database settings or code shows up as a failing test.

## What makes a test worth keeping

Whichever order you write them in, good tests share a few qualities.

- **It tests behaviour, not implementation.** It calls the public function or endpoint and checks the result, so refactoring doesn't break it.
- **It fails for one clear reason** and the message says what went wrong.
- **It's deterministic.** No sleeps hoping something finished; no dependence on test order or wall-clock time.
- **It's fast enough to run on every change.** Integration tests can be fast too: the blogging platform's whole suite, against a real database, takes seconds.

## Another false lead: coverage numbers

Coverage tells you what *wasn't* run, not what was tested well. Aim for the risky paths (authorisation, money, data deletion, concurrency) to be covered by tests that would fail if the behaviour changed, and don't chase a percentage.

> **Remember this**
> - The order matters less than what you test and against what.
> - Every bug fix starts with a failing test.
> - A mocked database proves nothing about SQL, permissions or concurrency.

## Case closed: what I'd do by default

1. **Every bug fix starts with a failing test.**
2. **TDD for parsers, rules and calculations**; tables of cases, plus properties or fuzzing.
3. **Integration tests against the real database and queue** for anything involving SQL, permissions, transactions or concurrency.
4. **A few end-to-end tests** for the main user journeys, not hundreds.
5. **Tests run in CI on every change**, and a flaky test is fixed or deleted, never ignored.

The real reward isn't a coverage badge. It's the freedom a good test suite gives you: to refactor boldly, fix bugs once, and trust that the thing you built still does what you meant it to.
