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
- Why it matters
- Three levels for a backend
- What to test first
- Check your understanding
- Apply it
- Review prompt
Lessons of Software Testing Fundamentals
- Why Automated Tests, and What to Test First Concept · 15 min
- Framework Essentials: Asserts, Fixtures and Parametrized Cases (pytest or Vitest) Worked example · 25 min
- Fakes, Stubs and Mocks Without Over-Mocking Concept · 20 min
- Lab: Testing Endpoints with a Test Client Lab · 25 min
- Reading Coverage and Choosing Test Levels Worked example · 15 min
- 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.
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 whereThe 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:
- 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.
- Pure logic with many cases. Validation, parsing and calculations are cheap to test and carry most of the edge cases.
- Paths that lose data or money. Duplicate charges, lost writes, overwriting the wrong record: test these before anything cosmetic.
- 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.
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_onfrom 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 TrueE + 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.08sThe 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:
return returned_on > due_on.. [100%]
2 passed in 0.04sTwo 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
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.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.