Skip to content

Tests Before There Is a Product

Early tests help you grasp a problem, even if the code is later replaced entirely.

≈ 9 min readSoftwareTesting

Tests Before There Is a Product

A test case needs two things: an input and the expectation of what should come out of it. Nothing more is needed to pin down a problem for which not a single line of code exists yet.

The reflex with something new goes in a different direction: build right away. An endpoint, a screen, a data model – something visible you can show. If you slow down by one step at this point and write the test first, you aren’t securing any code – it doesn’t even exist yet. The test forces you to name the problem in a single, verifiable sentence. And this sentence almost always outlives the implementation I write afterward and throw away a few days later. The test only becomes quality assurance later; at the beginning, it is a thinking tool.

Katas: practicing without building a product#

The concrete occasion for this text is katas. A kata is a small, clearly defined programming task that you don’t solve to get rid of it, but to practice on – the String Calculator kata, the Bowling Game kata, Roman numerals, FizzBuzz. You often already know the solution. You still do it again, in a coding dojo with the team or alone on a quiet morning, because it isn’t about the result, but about the path to it.

The interesting thing about a kata is that, deliberately, there is no product at the end. There is no customer, no deadline, no deployment. What remains is the experience of how a problem feels when you untangle it test step by test step. That is exactly the attitude I take into real projects. In the early phase of a feature, I often don’t know yet what the solution looks like. I only know which behavior has to be observable in the end. And that can be phrased as a test before a single line of production code exists.

Take the String Calculator kata. The task sounds trivial: a function that takes a string of numbers and returns their sum. The first test is as boring as it gets – and still the most important line of the day.

describe("String Calculator", () => {
  it("returns 0 for an empty string", () => {
    expect(add("")).to.equal(0);
  });

  it("sums two comma-separated numbers", () => {
    expect(add("1,2")).to.equal(3);
  });
});

Before I write add, I have made two decisions that were unspoken before: the empty case is not undefined, it returns the number 0. And the separator is, for now, the comma. Two small examples – and a discussion becomes possible that would otherwise only have come up in the code review or, worse, in production.

The test as a thinking tool#

In Test-Driven Development by Example, Kent Beck boiled this down to a simple loop: write a test that fails, get it green with the smallest possible step, then clean up. Red, green, refactor.

flowchart LR
  Red[Write a test,<br/>it fails] --> Green[smallest step<br/>to green]
  Green --> Refactor[clean up<br/>without changing behavior]
  Refactor --> Red

The decisive effect is not test coverage. It is the feedback on the design. If, for a single test case, I have to set up three objects, provide four dependencies and mock half a database, then the test is telling me something about my design, not about my diligence in testing: the boundary is in the wrong place. A test that is hard to write is almost always a sign of a coupling that I haven’t paid for yet but have already taken on.

That is why I write the test first, even when I know the code underneath is a throwaway piece. The test is the question; the code is only the first attempt at an answer. The question is what’s valuable. If I completely replace the first attempt at an answer after two days – because by then I understand the problem better – the question stays and checks the new answer just as strictly as the old one.

Specification by Example#

The second building block that, for me, belongs inseparably to this is Specification by Example, as Gojko Adzic describes it. The idea: instead of phrasing a requirement abstractly (“the discount applies to new customers”), you write it down as a concrete example that is domain language, expectation and automatable check all at once.

Scenario: A new customer gets the welcome discount
  Given a customer with no previous orders
  When the customer places an order of 100 EUR
  Then the invoice total is 90 EUR

This small scenario achieves more than it seems. It is written in a language a domain expert can read and correct. It pins the expectation down to a number you can argue about – is it really 90, or does the discount apply before or after VAT? And it can be bound to execution, so the description doesn’t go stale next to the code but runs along with it.

In practice, the example is often the moment a misunderstanding comes to light. For development, “new customer” meant “no order yet”, but for sales it meant “no order in twelve months”. As long as the requirement stayed abstract, everyone agreed. Only the concrete example with the 100 EUR made the two interpretations visible. That is exactly why I write examples early: not to test code, but to test assumptions nobody has voiced yet.

The boundary is the real decision#

When I phrase a test, I implicitly make an architecture decision: I define the boundary at which I check. And this boundary determines what I can change freely later and what I can’t.

If I check too deep – individual method calls, internal ordering, private helper structures – the test sticks to the implementation. Every restructuring that doesn’t even change the behavior turns it red. Such tests punish refactoring instead of protecting it, and the team quickly learns to avoid them or adjust them blindly. If, on the other hand, I check at a sensible boundary – the observable behavior of the unit, what another part of the system actually sees of it – then everything inside may change, as long as the promise holds on the outside.

This is the point where testing stops being purely a question of craft and becomes a design question. The boundary at which I test is the same boundary at which I can later cut open, swap out or replace the system. A well-placed test describes a seam in the system. A poorly placed test cements a detail that is nobody’s business from the outside. That is why, before the tool question – Mocha or Jasmine, Sinon or handwritten stubs – the question of substance pays off: Which behavior do I want to pin down, and which do I want to deliberately leave open?

When this pays off – and when it doesn’t#

I don’t want to claim that every line has to start with a test. A spike with which I only find out whether a library is any good at all doesn’t need a test – it is the experiment itself and gets deleted afterward. Neither does a pure end-to-end slice to see a layout.

The trade-off hinges on a single question: How well do I already understand the problem? If the task is clear and just needs typing, test-first costs more than it brings. If, on the other hand, I am not sure what “correct” even means – and in the early product phase, that is the normal case – then the test is the cheapest tool available to force clarity. Precisely because I know the code is throwaway, I want to capture the little I learn in a form that outlives the code.

Seen this way, test-first in the exploration phase doesn’t contradict throwing things away. It is the method with which I still extract lasting knowledge from a throwaway prototype: the one line that pins down the expected behavior stays, even when everything else goes.

The error path decides whether you can release#

There is a leap between “runs in the demo” and “runs in production”, and early tests make exactly this leap visible. In demo mode, almost every system looks clean, because you show the flow in which everything goes well: valid input, reachable service, exactly one run. It gets interesting next to that.

When I build an import, the exciting question isn’t whether it reads a correct file, but what happens with the half-broken one. If the run is repeated, do duplicates appear, or is it idempotent? What happens on a timeout in the middle of processing – does it abort cleanly, or does the system stay in a half-finished state? Which log line helps me at three in the morning to understand the error instead of just noticing it? These questions seem dry, but they are the real difference between a prototype and something you give to a customer.

That is why I write at least one test for the unpleasant case early on, not just for the pretty one. A test that checks that duplicate processing does not create two invoices says more about how well the design holds up than ten tests for the expected result. And it says so at a point when the change is still cheap.

What this measurably brings#

All of this would be folklore if it only produced a good feeling. The benefit is more concrete. Misunderstandings become visible in the example, not only at acceptance – that shortens the most expensive of all cycles, the one between “misunderstood” and “noticed that it was misunderstood”. Refactoring gets cheaper, because tests at the right boundary protect the behavior instead of freezing the implementation; the team dares to clean up instead of working around code. And handover gets easier, because an example in domain language is better documentation than any prose that goes stale next to the code.

If I had to pick out one effect, it would be this one: shorter feedback. An example that checks an assumption turns a discussion that would otherwise be held weeks later in production into a five-minute discussion at the whiteboard. That is not a soft improvement. It is the difference between a team that works on the problem and one that works on its misunderstandings.

What remains when the code goes#

Back to the beginning. The reason I write tests before there is a product is neither discipline nor completeness. It is that the test holds on to the one thing I want to keep while everything else is still in flux: the expected behavior, named at a boundary you can argue about.

The code of the first week is almost always wrong, and that’s fine – it was never meant as an answer, but as a question in executable form. When I throw it away and rewrite it, the same test checks the new version just as strictly. That is exactly why we practice on katas without ever building a product: what carries weight is not the result, but the judgment about which behavior counts. And this judgment is the only thing a team can really take with it when the first draft has long been deleted.

Revised and translated from German with the help of AI.