Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 15 min · revision 3

Why Automated Tests, and What to Test First

See what automated tests buy a backend team, learn the three test levels for a service, and pick the first tests to write with a red-green-refactor loop.

Contentsand lesson path

On this page

  1. Why it matters
  2. Three levels for a backend
  3. What to test first
  4. Check your understanding
  5. Apply it
  6. Review prompt

Lessons of Software Testing Fundamentals

  1. Why Automated Tests, and What to Test First Concept · 15 min
  2. Framework Essentials: Asserts, Fixtures and Parametrized Cases (pytest or Vitest) Worked example · 25 min
  3. Fakes, Stubs and Mocks Without Over-Mocking Concept · 20 min
  4. Lab: Testing Endpoints with a Test Client Lab · 25 min
  5. Reading Coverage and Choosing Test Levels Worked example · 15 min
  6. Check: Find the Missing Test Knowledge check · 8 questions

What you will learn

  • Choose the test level (unit, integration, end-to-end) for a behavior and use a coverage report to find untested branches.

Prerequisites: Python Foundations

01. Why it matters

Every change to a working service risks breaking a promise someone already relies on. The cheap way to find out is to click through the feature by hand; the honest way to describe that method is that it checks one path, once, on the day you remember to do it. Manual checking does not scale, and it is skipped exactly when deadlines press, which is when it matters most.

An automated test is a small program that runs a piece of your code with an input you choose, then compares the result with the outcome you promised. A suite of such programs runs in seconds and never gets bored, and as long as each test fixes its inputs (the date, random values, the data it reads), it gives the same answer on every run. When it is wired into your workflow, a change that breaks a promise is reported before anyone else sees it.

A regression is a change that breaks behaviour that used to work. The whole point of a test suite is to catch regressions early, while the causing change is still on your screen. You met this idea in the frontend testing skill, where the levels were unit, component and end-to-end (see the first lesson of that skill); a backend service has the same shape with different names, and this lesson maps it.

02. Three levels for a backend

A unit test exercises one function, method or class in isolation, in memory, with no network and no database. It runs in milliseconds and, when it fails, points at a small piece of code. Most backend logic wants to live here: input validation, calculations, mapping between formats, and the rules inside your domain.

An integration test exercises several parts together, because some behaviour only exists where the parts meet: a handler plus request validation plus a real database, or your service plus a cache. These tests are slower because they start real things, often in containers, but they catch wiring mistakes and constraint bugs that unit tests cannot see.

An end-to-end test drives the deployed system through its public interface: a browser journey, or a script calling the running API the way a customer would. It is the only level that exercises the whole deployed system together, and it is the slowest and least precise, because a failure anywhere in the journey turns it red.

The three levels for a backend service
Level        Runs with              Good at                 Weak spots
unit         nothing else           rules, parsing, maths   cannot see mistakes in how
                                                            parts connect
integration  database, other parts  queries, constraints,   slower; needs a database
                                    request handling
end-to-end   the deployed system    one real user journey   takes minutes; a failure
                                                            does not say where

The rule for placing a check is the same as on the frontend: put it at the lowest level that can see the behaviour. A rounding rule does not need a database to fail; a UNIQUE constraint does, because only the database enforces it. A backend holds more pure logic than a user interface does, so a healthy backend suite leans further towards unit tests than a frontend suite can. The later lessons of this skill practise all three: unit tests first, then endpoint tests that run your real request handling in-process, while journeys over a deployed system belong to the api-integration-testing skill.

03. What to test first

Nobody writes every test at once, so order matters. Rank by risk:

  1. Known bugs first. Every bug report is a test that should have existed. Write a test that reproduces the report, watch it fail, fix the code, watch it pass, and keep the test forever as a guard against the bug returning.
  2. Pure logic with many cases. Validation, parsing and calculations are cheap to test and carry most of the edge cases.
  3. Paths that lose data or money. Duplicate charges, lost writes, overwriting the wrong record: test these before anything cosmetic.
  4. Error paths. The code that handles bad input and failure decays unseen, because nobody exercises it by hand.

This ordering has a loop with a name: red, green, refactor. Red: write a small failing test that expresses the behaviour you want. Green: make it pass with the simplest change that works. Refactor: clean up the code, with the test guarding the behaviour. You already ran this loop with pytest in the final python-foundations lesson; here is the same loop used to fix a reported bug.

Python · library.py (as reported: returns on the due day)
from datetime import date


def is_overdue(due_on: date, returned_on: date) -> bool:
    """True when a book came back after its due date."""
    return returned_on >= due_on
Python · test_library.py
from datetime import date

from library import is_overdue


def test_returning_on_the_due_day_is_not_overdue() -> None:
    assert is_overdue(date(2026, 10, 23), date(2026, 10, 23)) is False


def test_returning_after_the_due_day_is_overdue() -> None:
    assert is_overdue(date(2026, 10, 23), date(2026, 10, 25)) is True
Illustrative output (red run of pytest -q, excerpt; the run time varies)
E        +  where True = is_overdue(datetime.date(2026, 10, 23),
datetime.date(2026, 10, 23))
FAILED test_library.py::test_returning_on_the_due_day_is_not_overdue - assert...
1 failed, 1 passed in 0.08s

The failing test is the bug report in runnable form: returning a book on its due day counted as late because of >=. The red run matters more than it looks. Until you have seen the test fail for exactly this reason, you cannot know it would have caught the bug; a test written after the fix, and never seen failing, is a hope rather than a check. Now make it green by comparing with > instead:

Python · library.py (the fix, one operator)
    return returned_on > due_on
Illustrative output (green run of pytest -q; the run time varies)
..                                                                       [100%]
2 passed in 0.04s

Two habits make the loop pay. First, the red test and the fix travel in one small change set, with a commit message that names the behaviour, as the git hygiene lesson practises. Second, the suite runs on every push automatically, which is what the continuous-integration skill sets up; a test that only runs when you remember to run it is no better than checking by hand.

04. Check your understanding

Practice check · 3 questions

Where tests go, and what comes first

Three situations from a team adding a suite to an older service.

Q1. You have one hour to add tests to an untested payments service before a risky change. What do you test first?
Q2. Every unit test of add_reader passes with its own in-memory list, yet production shows duplicate readers. What was missing?
Q3. A user reports the overdue off-by-one bug. Following red-green-refactor, what do you write first?
Practice only: not graded, not saved, not evidence.

05. Apply it

Take the reading-list service you will build in this skill (or a project of your own). Write down five regressions you would be afraid of, such as a title stored twice, an unknown book returning 200, or an invalid rating crashing a request. Next to each, note the lowest level that can catch it: unit, integration or end-to-end.

Then pick one real bug from your own project, or reintroduce the >= bug above, and run the full red-green-refactor loop: failing test, fix, green run, and a commit whose message names the behaviour.

06. Review prompt

Without looking back: name the three test levels for a backend and one thing each can see that the others cannot. State the rule for choosing where a check belongs. Then list the four kinds of behaviour to test first, in order, and explain why a test must be seen failing before you trust that it guards the fix.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check