Course contents · Lesson 14 of 17

Software Testing Basics: Why We Test Before We Ship

Testing and Quality · Lesson 14 of 17 · 11 min Engineer

On Friday evening a one-line change to a shop's checkout goes live. By Saturday morning customers report that discount codes take off twice the right amount, and the team spends the weekend refunding orders. A three-line test would have caught it before it shipped. This lesson covers software testing basics: why teams test, what a test is made of, what makes one good and what tests can never prove.

Why test before you ship

A test is a small piece of code that runs part of your program with inputs you chose and checks that the result matches what you expected. As What Is Software? put it, a bug is a gap between what was written and what was meant. A test writes down what was meant, so the computer can spot the gap for you. Teams test for three reasons.

Find bugs early. Caught while coding, a bug costs you minutes. Caught in code review, it also takes a reviewer's time; after a release, it needs a new release. In production, the live system customers use, it adds unhappy users and a hotfix, an emergency fix rushed straight to production. The chart shows the shape; its bar heights are illustrative.

Illustrative bar chart of the cost to fix the same bug by the stage where it is found. Coding, you in minutes, is a very short bar marked a test catches it here. Review, plus a reviewer, is a little taller. Release, plus a new release, is much taller. Production, plus customers and a hotfix, is the tallest. A banner says later finds involve more people and more steps
Each later stage adds people and steps, so the cheapest catch is the tasting spoon: a test run while coding.

Change code with confidence. A regression is a bug where something that used to work breaks after a change. A test suite, all of a project's tests together, lets you recheck every tested behaviour after each change. That safety net is what lets teams refactor: restructure code without changing what it does.

Document what the code is meant to do. A test such as "a 10 percent discount on ₹500 gives ₹450" is an exact example of intended behaviour. Unlike a comment, it cannot silently go stale: if the code stops matching it, the test fails.

Anatomy of a test: arrange, act, assert

Most tests follow three steps, a pattern called arrange-act-assert. Arrange prepares the inputs and anything else the code needs. Act calls the code under test, once. Assert compares the actual result with the expected one. An assertion is a statement that must be true; if it is false, the test fails.

Here is a small Python function, add, and a test for it. Python's assert statement does nothing when its condition is true. When it is false, it raises an AssertionError, Python's error for a failed check:

def add(a, b):
    return a + b

def test_add_returns_sum():
    a, b = 2, 3          # arrange
    result = add(a, b)   # act
    assert result == 5   # assert

The assertion alone decides pass or fail, as the diagram shows:

On the left, the code under test: def add(a, b), return a + b. On the right, the test function test_add_returns_sum in three numbered steps. 1, Arrange, set up the inputs: a, b = 2, 3. 2, Act, call the code once: result = add(a, b), with an arrow labelled calls going to the function and one labelled returns 5 coming back. 3, Assert, check the result: assert result == 5. Below, true means the test passes, and false means AssertionError, the test fails
A test is ordinary code that calls the function, and one assertion turns the result into pass or fail.

A test framework finds your tests, runs them all and lists every failure; pytest for Python, JUnit for Java and Jest for JavaScript are common ones. pytest, for example, collects functions whose names start with test from files named test_*.py or *_test.py.

Manual versus automated tests

A manual test is a person using the app and checking it by eye: open the checkout, type a code, read the total. People notice the unexpected, such as a hidden button. But manual testing is slow, and a tired person skips a step or misreads a number.

An automated test is written as code, like the one above, and a program runs and checks it with no person involved. Hundreds of small ones can run in seconds, the same way every time, on every change. That is why the checks on a pull request, which you met in Branches and Pull Requests, usually include the test suite.

Teams use both: automated tests guard what is already known to matter, and people explore what is new. Day to day, running the suite becomes a loop:

Flowchart. Write code, or change it, leads to run the tests, then a decision: all pass? Yes leads to commit, with green tests. No leads to read the failure: which test, what values. From there, one arrow leads to fix the code, the usual case, and another to fix the test, only if the behaviour changed. Both lead back to run the tests. A banner says edit a test only if the behaviour changed
A red test is a message: read it, fix what is wrong and commit only when everything is green.

When a test fails, read its message first. Usually the code is wrong. Sometimes the test is, because the expected behaviour changed on purpose; then update the test. Never edit a test just to make it pass: then it checks nothing.

What makes a good test

A test helps only if people trust it and run it. Good tests are:

  • Fast. It runs in milliseconds, so you can run the whole suite after every small change.
  • Repeatable. It gives the same result on every run of the same code. A test that depends on the clock, a random number or a live website can fail when nothing changed. Such a test is called flaky, and people soon learn to ignore it.
  • Independent. It sets up its own data and does not rely on another test running first, so tests can run alone or in any order.
  • Clear about what failed. Its name and message point at the problem. test_discount_on_500_gives_450 failing with "expected 450, got 400" tells you where to look; test_2 failing with a bare AssertionError does not.

Good tests also check one behaviour each, so a failure has one meaning. And they cover edge cases, unusual inputs at the limits such as zero or an empty list, which the code review lesson named as where bugs hide.

What tests cannot tell you

A test checks only the cases someone wrote. The computer scientist Edsger Dijkstra put it this way: program testing can show the presence of bugs, but never their absence. A green suite means "no bug found in these cases", not "no bugs".

Code coverage is the share of your code that ran during the tests, usually counted in lines; tools such as Coverage.py measure it. Low coverage is a useful warning, because code that no test runs is never checked. High coverage is not proof of quality, because running a line is not the same as checking its result.

Suppose add is broken and multiplies. A test of add(2, 2) == 4 still passes, since 2 × 2 is also 4. Every line ran, so coverage is 100 percent, and the bug remains. Good tests come from asking which inputs could expose a mistake, not from chasing a number. The next lesson compares tests of one function with tests of the whole system.

Check yourself

A teammate says tests slow the team down because each takes ten minutes to write. What does that leave out?

The test then runs on every later change almost for free, and a bug it catches while coding costs minutes, not a hotfix after customers find it.

A test passes most days but fails every Monday, with no code change. Which quality is it missing, and what should you check first?

It is not repeatable, so it is flaky. Check whether it reads today's date or the clock: the arrange step should set a fixed date, so every run sees the same input.

Your project has 100 percent test coverage. Can a bug still reach production?

Yes. Coverage says every line ran during the tests, not that the assertions checked the right results; a multiplying add passes add(2, 2) == 4 with every line covered.