Inherited a codebase with no tests? Where to start

How to add tests to an untested app without stopping feature work: begin with the flows that earn money or lose data, and test at the cheapest level.

The short answer

Do not try to reach a coverage number. Write a handful of tests around the flows where a bug costs money, loses data or exposes data, test each at the cheapest level that proves it (usually the API), and add a test every time you fix a bug. Add static checks like a type-aware linter first, because they find real bugs with no test code.

On this page
  1. What should you do before writing any test?
  2. Which flows get tests first?
  3. At what level should you test?
  4. How do you test code that was not built for it?
  5. How do you keep it going?
  6. What about property tests?
  7. What does a good first month look like?
  8. What should you avoid?
  9. Working with us

You inherited an app with no tests. Changing anything feels dangerous, because nothing tells you what you broke. The goal is not a high coverage percentage. The goal is to be able to change the app without fear, in the places where fear is justified.

What should you do before writing any test?

Add checks that need no test code. They are the cheapest bugs you will ever catch.

  1. A type checker. If the language supports types, turn them on and fix errors in steps.
  2. A linter with type-aware rules. On one of our products, the first run found 2,319 violations; sorting them took one pass and afterwards the linter’s output meant one thing: something new is wrong. We wrote it up in linting a grown codebase to zero.
  3. Unused-code detection. A tool that fails on an unused or unlisted dependency finds problems that work only by accident.
  4. A formatter, so diffs show real changes.

These run in seconds on every pull request and need no knowledge of what the app does.

Which flows get tests first?

Rank flows by the cost of a bug, and test from the top:

Priority Examples
Money Checkout, subscription changes, refunds, invoices
Data loss or exposure Permissions, tenant separation, deletion, export
Sign-in Login, password reset, sessions
The core job of the product The one thing customers pay for
Anything that failed before Every past incident

Skip tests for pages that merely render, constants that restate themselves and mocks that return what they were told. A test must be able to catch a bug that nothing else catches.

At what level should you test?

Test each rule once, at the cheapest level that proves it. For most apps, that is the API level.

  • API or integration tests against a real database give the most protection for the least effort. They call real endpoints and check responses and rows. They do not care how the code inside is organized, so they survive refactoring.
  • Unit tests suit pure logic: price calculations, date rules, parsers.
  • End-to-end browser tests cover user flows and browser-only behavior. Keep them few: sign up, the main flow, payment. Do not re-test every validation message in the browser.

Use a real PostgreSQL in tests, not a mock. A mock cannot tell you whether the database enforces a rule or a constraint.

How do you test code that was not built for it?

  • Characterization tests first. Before changing messy code, write a test that records what it does today, including its odd results. Then change the code and see what differs. The differences are your decisions to make.
  • Compare old and new. When changing a lot, run the old and new versions on the same inputs and diff the outputs. We did this for a database switch, page by page; see our SQLite to PostgreSQL move.
  • Seed data. A small, realistic set of data makes tests quick to write and repeat.
  • Seams. Where code calls an external service directly, put a thin wrapper around the call so a test can replace it.

How do you keep it going?

The rule that works best: every bug fix comes with a test that fails before the fix and passes after. In a few months, the suite covers the places that actually break. It also matches how you spend time: tests accumulate where the bugs are.

Run the tests for what you touched while you work, and the full suite before merging. Put it in CI, with the main branch protected so a red build cannot merge.

Two habits keep a young suite healthy:

  • Never wait for the clock. To test an expired link, create one that is already expired. One fixed 62-second wait cost us more than 81 UI sign-ins combined; see our CI and test lessons.
  • Treat a flaky test as a bug, not as something to retry until it goes green.

What about property tests?

Use them where logic has state: balances, queues, state machines. Random sequences of operations from a logged seed, checked against invariants, find cases nobody wrote by hand. A failing seed becomes a regression test. We use this for status columns; see status columns are state machines.

What does a good first month look like?

  • Week 1: type checker, linter and CI, with a protected main branch.
  • Week 2: API tests for sign-in and the permission rules.
  • Week 3: API tests for the money flow, and one end-to-end test of the core flow.
  • Week 4 onward: a test with every bug fix, and a test before every risky change.

What should you avoid?

Avoid a coverage target, because it rewards tests that execute code without checking anything. Avoid tests that restate the implementation, such as a constant equal to itself or a mock that returns what it was told. Avoid duplicating one rule across unit, API and browser tests; if the API test proves it, stop there. And avoid a freeze: a month of only writing tests loses the team’s trust. Put the first tests around the next feature you are going to change, so the work pays for itself at once.

Working with us

If you have an app with no safety net and a roadmap that cannot wait, we can add checks and tests around the risky flows while features continue. Tell us about the app through contact, or read about our web application work.

Keep reading

Tell us what you want to build.

A product, a website, a search problem or a system that should work better. We reply with a plain answer and the first step.

A few sentences are enough (at least 20 characters).

We use your details only to answer your inquiry. See our privacy policy.