TDD: the test list before the code

level 2 · 20 min · test list (TDD)

In TDD, the test comes before the code: you pick the next behavior, write a failing test, write the minimum to pass and clean up. What decides the next test is the test list, the inventory of what the code has to do.

Whoever starts programming from the two tests already on screen finishes early and wrong. The list comes from the rules in the brief: each rule becomes at least one case with an input and an expected output, and whatever is trickiest about it (edges, repetitions, empty values) becomes the next case.

Rule of thumb. Before programming, turn each rule into a test case with an input and an output. Then pick one at a time: make it pass, and only then go to the next.

Build the test list

The function slugify(title) turns an article title into the end of a URL. These are the rules in the brief as a test list. The two visible tests cover only the first two rows.

Rules from the brief as test cases
RuleInputExpected outputVisible tests
Lowercase, words separated by hyphens"Hello World""hello-world"covers
Digits stay"Top 10""top-10"covers
More than two words"Testing in production""testing-in-production"does not cover
Repeated spaces and spaces at the ends become a single hyphen, with no hyphen at the ends" many spaces ""many-spaces"does not cover
Punctuation is dropped"What's new?""whats-new"does not cover
Accents are removed"Café com leite""cafe-com-leite"does not cover
A hyphen in the title is a separator, not doubled"Before - after""before-after"does not cover
Blank title" """does not cover

Exercise: write the code

Your code must pass the visible tests and the hidden checks, which cover the rest of the list.

Write slugify(title), which turns a post title into the last part of its URL.

Rules: lowercase; words separated by a single hyphen; accents removed ("é" becomes "e"); anything that is not a letter, digit or space is dropped; no hyphen at the start or end; an empty or blank title gives "".

The two visible tests cover only the first rule. Hidden checks cover the rest, so read the rules as a test list and work through it.

Visible tests

test("lowercases and joins two words", () => {
  expect(slugify("Hello World")).to.equal("hello-world");
});

test("keeps digits", () => {
  expect(slugify("Top 10")).to.equal("top-10");
});

Your code

References

  • ISTQB® Certified Tester Foundation Level v4.0, section 2.1, Testing in the Context of a Software Development Lifecycle. © ISTQB®.