Tech●●●●●Difficulty 3 of 5

How does software test itself?

Programmers write programs whose only job is to check other programs, and a red bar tells them something broke.

▶ Start the story

Software tests itself through small programs that check the main program. A programmer writes a : a short piece of code that runs one small part of the software with a known input and checks that the result matches the answer they expect. If it does not, the test fails and reports it. Because the checking is automated, it can be run often and without staffing cost.

The pattern is simple: the test, the function it tries, and the expected value. The programmer writes into the test a result that is known to be good. When the tests run, a testing logs every one that fails and gives a summary. Many tools show a bar that is green when everything passes and turns red when any test fails, which is where the phrase green build comes from.

What a test run tells you

Green

  • Every expected value matched
  • Safe to carry on and merge

Red

  • At least one test failed
  • The summary says which one

This matters because a program can run perfectly well without doing the right thing. is too slow for software that changes all day. Some teams go further and write the failing test first, then just enough code to make it pass, then tidy up and repeat. This is called test-driven development, and Kent Beck, who is credited with developing or rediscovering it, says it came from an old programming book.

Tests are not magic. Programmers often write 3 to 5 lines of test code for every line of code, and no amount of testing can check every path through a program. But a good lets you change code with confidence. Martin Fowler imagines a mischievous imp that can secretly alter the code but not the tests: if the tests stay green, they are not good enough.

3 to 5

lines of test code programmers often write for every line of code

Quiz me

0/3

  1. 1.What does a unit test usually compare?
  2. 2.What does Martin Fowler's mischievous imp test?
  3. 3.Why add a test whenever a bug is fixed?

Recap

Software tests itself by running small automated checks with known answers, and a red bar means something broke.

💡 A trick to remember it · Green bar, go; red bar, whoa: a failing test is a smoke alarm for code.

Surprising fact · Programmers often write 3 to 5 lines of test code for every line of code.

Sources (4)

No source, no claim. Every fact in this lesson (16 claims) cites at least one of these.

  1. [1]Unit testing · Wikipedia
  2. [2]Continuous Integration · martinfowler.com
  3. [3]Test-driven development · Wikipedia
  4. [4]Continuous integration · Wikipedia
More lessons in 💻 Tech (3) See all tech lessons →

One more light on your map.

Get one lesson like this every day, about the things you love. Free, in two or five minutes.

Get the share card for this lesson ↗