Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
PHP Development

What to Test When You Cannot Test Everything

Last updated:

Coverage is the wrong target

Chasing a coverage percentage produces tests of trivial code and leaves the complicated parts untested, because they are harder to write tests for.

The useful question is not how much is tested but whether you could change the code confidently.

What to test first

  1. Calculations — pricing, tax, commission, anything with arithmetic
  2. Money paths — payment, refunds, invoicing
  3. Authorisation — who can see and do what
  4. Integrations — what happens when the other system fails
  5. Anything that has broken before
The last one is the most useful heuristic. A bug that has occurred once will occur again unless a test prevents it.

Different tests for different jobs

TypeUse for
Unit testsCalculations and business rules
Integration testsDatabase interactions and queries
Feature testsComplete user journeys
Contract testsExternal API expectations

A small number of feature tests covering the main journeys catches more real problems than a large number of unit tests on trivial methods.

Make them run automatically

Tests that require someone to remember to run them stop being run within a month. They should run on every push, automatically, with a visible result.

That is a day of setup and it is what turns a test suite from a nice idea into a working safety net.

Testing legacy code

  • Start with characterisation tests — capture current behaviour
  • Test at the highest level you can, since units may be untestable
  • Add tests when you touch a piece of code, not as a separate project
  • Accept partial coverage; it is better than none

Frequently asked questions

How much testing is enough?

Enough that you can change the code without fear. That is usually far less than complete coverage and far more than nothing.

Does testing slow development?

Initially. It speeds it up considerably once the application is large enough that manual verification is impractical.

Can we add tests to a legacy application?

Yes, incrementally. Characterisation tests around what you are about to change is the practical approach.

Should tests use a real database?

For integration tests, yes — a test database. Mocking the database tests your mocks rather than your queries.

Keep reading

Afraid to change your own application?

That fear is what tests remove. Start with the calculations and the money paths.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

Web DevelopmentCustom Software Development