Skip to main content
Jimmy Jansen

A Pragmatic Testing Strategy

I write tests so I can change code without checking everything by hand. A failed test should make it clear what broke.

I like having tests running while I refactor difficult code. They help me spot when I’ve broken something elsewhere, so I can keep working without repeating the same manual checks.

I don’t think every line deserves a test. A test that gives us no useful feedback is still another piece of code to maintain.

Link to section: Start with the failureStart with the failure

Before I write a test, I want to know what could go wrong and how much that would matter. With a sliding window, the boundary cases are fiddly enough that I can easily make a mistake. A permission check may be much easier to write, but getting it wrong could expose another customer’s data. I’d want that rule tested even if I was confident about the code.

Coverage helps me find gaps in those checks. It shows code the suite never runs, but it doesn’t tell me whether the tests would catch a wrong result. A snapshot test can stay green after a bad change if we accept the update without reviewing it.

Link to section: Make a difficult system safer to changeMake a difficult system safer to change

At Rangle, I led the recovery of a legacy application that was going down for hours every day. We needed to stop those outages, but there were almost no tests to tell us whether a database change had broken something elsewhere. Covering more than ten million lines of code before making the repairs wasn’t realistic.

We focused on the workflows customers depended on. I paired with engineers to set up a production-like environment and black-box tests around those workflows, giving us a way to check the application’s behaviour while we changed the database underneath it.

The team reduced the immediate database load while I worked through the upgrade path and production runbook. We used the workflow tests to validate the changes before deployment. That let us make the repairs without first having to test every part of the application. The recurring daily outages stopped during the eight-week engagement.

Link to section: Choose how much of the system to testChoose how much of the system to test

Once I know what I’m trying to catch, I decide how much of the system needs to be real. A mock makes a test easier to set up, but it means I’m relying on my understanding of how that dependency behaves. At some point I need to check that understanding against the real thing.

If setting up the test is unexpectedly difficult, I look at the design before adding more mocks. A calculation that needs a database, or one decision that needs fifteen mocks, may be a sign that we’ve mixed things together that would be easier to work with separately. Some code really is awkward to test, so I want to understand why.

A focused test against a real database can be easier to run and understand than a test held together by a pile of mocks. I care about how quickly I get useful feedback and how hard it is to work out what failed.

People use these labels differently. This is what I mean by them here.

Link to section: Unit testsUnit tests

These are for logic I can exercise on its own, such as a calculation or a business rule. I can try lots of cases without setting up the rest of the application, and I like keeping them running while I work.

The expected result needs to come from the rule we’re implementing. Copying the calculation into the assertion can give us the same bug twice.

Link to section: Component testsComponent tests

I use component tests a lot because they let me check how several parts of the application work together without starting the whole environment. The test runs the application code, with mocks or stubs in place of the database and external services.

I check how the application uses those dependencies, including the calls it makes and how it handles their responses. Calling a mock directly and checking the return value I’ve configured only tests my setup.

A passing component test still can’t tell me whether the database or service behaves the way I’ve mocked it.

Link to section: Integration testsIntegration tests

Here I use a real dependency to check the interaction: does the query work against the database, or does the application handle the service’s actual response?

For a database migration, that means testing against the relevant schema. I keep these checks focused so it’s easier to tell whether a failure comes from the application, the dependency or the test setup.

Link to section: E2E testsE2E tests

These follow a user journey through the system, using a real interface and application services with realistic data. Being able to log in and access something you’ve paid for depends on the pieces working together, so I want tests that follow those paths.

I use browser automation to check a feature as I build it, but I’m more selective about what stays in CI. A large browser suite can be slow to run, and when a test fails it can be hard to work out which part of the system caused it.

I keep the important user journeys covered at this level. Detailed rules and edge cases are usually easier to check closer to the code responsible for them.

Link to section: Testing while I workTesting while I work

For data-heavy work, I like test-driven development (TDD). I can define cases and build up the implementation incrementally. When I don’t understand the problem yet, I need to explore the code first. I’ll write something, iterate on it, then write the tests once I understand the behaviour I need. I don’t see a reason to force both kinds of work through the same process.

Link to section: Leave room for QA to surprise youLeave room for QA to surprise you

My tests can carry the same assumptions as my code. I might test a feature in one tab and miss what happens when a user opens a second. QA can explore those other paths and question whether the behaviour makes sense, even when every automated check passes.

Once we understand a failure QA has found, we can write a regression test for it. A focused component test may catch it just as well as a browser test, with less setup. Automating that check gives QA more time to explore the cases we haven’t considered.

Link to section: Start with the team’s problemStart with the team’s problem

The first useful tests in that recovery covered customer workflows while we repaired the database. A change to an isolated calculation would need a different approach. I want the risk in the work to determine where we start.

If I’m helping a team improve its tests, I’d use the next difficult change as the starting point. We can work out which failure would matter, then build a check that would catch it. A failure should help the engineer understand what needs attention, and a passing result should address the risk we wanted to check.

We can judge that feedback together while making the change. If the useful check is buried in a suite that keeps raising false alarms, fixing those failures may matter more than adding more tests. The team needs checks it can trust when deciding whether to ship.

A deadline can still mean leaving something unchecked. I want that choice to be explicit, with the cost of a failure understood by the people who would have to deal with it. A green suite cannot make that decision for us.