All courses

What automated tests are and how to run them

Explain a test as code that runs your code with known inputs and checks the output. Run a project's test suite with one command and read which tests passed and which failed.

Builds on: Command output and exit codes, How a code project is laid out

Read it card by card Start the whole course

A tests folder you've never run

You've downloaded a small project so you can try changing it. The README says: *Run the tests before and after any change.* There's a `tests/` folder full of files named like `test_prices.py`. You've never run a test. What is actually inside those files? And what single command makes them all run and tell you whether things still work?

Code that checks other code

A **test** is a small piece of code. It calls your code with an input whose right answer you already know, then checks the result. For a function `apply_discount(price, percent)`, a test might say `assert apply_discount(100, 10) == 90`. `assert` means *this must be true*. If it is true, the test passes. If not, it fails and shows what it got. A test only checks the code it calls, with the inputs it uses. Because tests are code, a machine reruns hundreds in seconds. That's the **automated** part.

Running a test suite: one command

All of a project's tests together form its **test suite**. A **test runner** finds them by their names (for example, any file whose name starts with `test_` and ends in `.py`), runs each one, and reports. 1. `cd` to the project root. 2. Find the command in the README or config. Common ones: `pytest` (Python), `npm test` (JavaScript), `go test ./...` (Go), `cargo test` (Rust). 3. Run it and read the summary line at the bottom. 4. Exit code 0 = all passed. Non-zero = a failure, or the tests couldn't run.

Reading a run with one failure

You run `pytest` from the project root (output on the next card). `collected 3 items` means it found 3 tests. In `.F.`, each character is one test: `.` passed, `F` failed. Under FAILURES, pytest names the failing test. The `>` line is the check that failed. The `E` line repeats it with the call replaced by what it actually returned. So `assert 10.0 == 90` means `apply_discount(100, 10)` returned `10.0` where the test expected `90`. The bottom line gives the totals: 1 failed, 2 passed. `echo $?` prints `1`, so the run as a whole failed.

A pytest run, one test failing

Progress is at the top (`[100%]` means all collected tests have run). The failure detail is in the middle and the totals are at the bottom. The last command shows the exit code.
$ pytest
collected 3 items

tests/test_prices.py .F.          [100%]
========== FAILURES ==========
_________ test_discount _________
>   assert apply_discount(100, 10) == 90
E   assert 10.0 == 90
===== short test summary info =====
FAILED tests/test_prices.py::test_discount
===== 1 failed, 2 passed in 0.04s =====
$ echo $?
1

Known input, expected output, one command

A test runs your code on a known input and checks the answer. Run the whole suite from the project root with the project's one command (`pytest`, `npm test`, …). Then read the bottom summary line and the exit code. For each failure, compare what came back (the `E` line) with what the test expected. A pass only covers what the tests actually check.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.