Course contents · Lesson 15 of 17

Unit vs Integration vs End-to-End Tests Explained

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

Your shop's 400 automated tests are green, yet on sale day customers find that the discount code applied in the cart has vanished by the payment page. Each function worked alone; nothing tested the whole journey. This lesson explains unit vs integration vs end-to-end tests: what each checks, what it costs, how many to write and when to use a stand-in for a real part.

Unit tests: one part on the bench

A unit test checks one small piece of code, such as a function or a class, in isolation: only that piece runs, with no real database, network or files. It uses the arrange-act-assert pattern from Software Testing Basics.

Here is a backend function that totals a cart in rupees after a percentage discount, with a pytest unit test for it:

def cart_total(prices, discount_percent):
    return sum(prices) * (100 - discount_percent) / 100

def test_cart_total_applies_ten_percent_discount():
    assert cart_total([300, 200], 10) == 450

Nothing waits for a disk or a network, so the test takes milliseconds, and a project can run thousands like it after every change. When one fails, you know which function to open. The limit is the spark plug's: the test proves that the piece works alone, not that it fits the pieces around it.

Integration tests: parts working together

An integration test checks that several parts work together, such as your code and a real database, a file or another service. It is the engine on the test stand. A typical one calls the shop's save_order function with a real database, then reads the table back to check the new row.

Bugs hide where parts meet: a query that names a column the table lacks, or an API field renamed on one side only. Only a test that runs both sides can see them. The cost is speed and setup: starting a database and creating tables makes each test take milliseconds to seconds.

Use the same database engine as production when you can. SQLite, a small engine that comes with Python, accepts some values that PostgreSQL rejects, such as text in a whole-number column, so a PostgreSQL team often tests against a real PostgreSQL in a Docker container.

End-to-end tests: the test drive

An end-to-end test checks the whole system the way a user does, through the real interface, with the frontend, backend and database all running. Tools such as Playwright, Selenium and Cypress drive a real browser. In this Playwright for Python test, the pytest-playwright add-on supplies page, a browser tab with no visible window by default. Names are illustrative:

from playwright.sync_api import Page, expect

def test_customer_can_buy_a_kettle(page: Page):
    page.goto("https://shop.example.com/")
    page.get_by_role("button", name="Add to cart").click()
    page.get_by_role("link", name="Checkout").click()
    page.get_by_role("button", name="Pay").click()
    expect(page.get_by_role("heading", name="Order confirmed")).to_be_visible()

get_by_role finds an element by its role, such as a button, and its name, as a person would, and expect waits, up to a time limit, for the heading to appear.

Only this level proves the customer's journey works. But it is the slowest, often seconds per test, because a real browser loads real pages. It is also fragile: a slow network can make it fail at random on unchanged code, which the last lesson called flaky, and a renamed button breaks it although checkout works. A red result says only "checkout is broken", like a rattle on the road.

The diagram marks each test's boundary on one shop: the parts that run for real.

An online shop from left to right: a customer, a browser showing the checkout page, a backend holding two functions, cart_total and save_order, and a database that save_order sends SQL to. A small green dashed box around cart_total alone is labelled unit test. A purple dashed box around save_order and the database is labelled integration test. A large pink dashed box around everything from the customer to the database is labelled end-to-end test. A banner says a wider boundary proves more, runs slower and is harder to debug
The wider a test's boundary, the more it proves, but the slower it runs and the harder its failures are to trace.

The test pyramid: many small tests, few large ones

The test pyramid is the usual rule of thumb for the mix: many unit tests, fewer integration tests and very few end-to-end tests. Unit tests are cheap, quick and precise, so they do most of the checking. Each layer above adds only what the one below cannot prove: first that parts fit, then that the whole journey works.

A pyramid in three layers. The narrow top is end-to-end tests: very few, seconds to minutes each, the highest cost and often flaky. The middle is integration tests: fewer, milliseconds to seconds each, medium cost because they need real parts. The wide base is unit tests: many, often thousands, milliseconds each, the lowest cost, and a failure names the piece. A banner says higher proves more, lower runs faster and cheaper. The speeds are labelled typical and illustrative
Each step up proves more per test but costs more, so the counts shrink.

Turn it upside down, a shape often called the ice-cream cone, and the suite is mostly end-to-end and manual tests. It takes hours, so people run it rarely. Flaky failures teach them to ignore red, and each real failure starts a hunt for the broken part. The pyramid sets proportions, not exact counts; Automated Testing in CI shows pipelines running the cheapest level first.

Choosing the level, and when to use test doubles

For each behaviour, and each bug you fix, write the test at the lowest level that can catch it. The flowchart asks the questions cheapest first:

Flowchart. A bug in checkout, reported by a customer, leads to the question: is it one function's logic? Yes leads to a unit test, for example the discount maths is wrong, the cheapest. No leads to: do two parts disagree? Yes leads to an integration test, for example SQL with a wrong column, slower. No leads to: does it show only in the full journey? Yes leads to an end-to-end test, for example a coupon lost between pages, the slowest. No leads to find the cause first, then ask again, which loops back to the start. A banner says write the test at the cheapest level that catches it
If a unit test can catch the bug, an end-to-end test is the slow, costly way to catch it.

Most bugs are logic, so most tests end up as unit tests. Keep end-to-end tests for journeys that would hurt if they broke, such as sign-up and payment.

A test double is a stand-in for a real part during a test, as a stunt double stands in for an actor. Use one when the real part is slow, costs money or behaves differently on each run, such as a payment service, an email sender or the clock.

A fake is a simple working version of the part, such as a list in memory that stores orders in place of a database. A mock, such as Mock from Python's unittest.mock module, returns answers you set in advance and records its calls, so a test can check that the payment service was charged once with ₹450.

The risk is that a double behaves the way you believe the real part does. If you are wrong, the unit test passes and production fails, so keep some integration tests against the real thing, as the try-it shows.

Check yourself

A 15 percent discount gives the wrong total, and you have traced the bug to cart_total. Which test should you add, and why not an end-to-end test?

A unit test: the bug is logic inside one function, so a unit test reproduces it in milliseconds and points at it. An end-to-end test would be slower and would not say where.

Unit tests that use a mock database all pass, yet saving an order fails in production with no such column: price. Why did the tests miss it?

The mock accepted any SQL and returned what it was told, so the query never reached a real engine. An integration test that runs save_order against a real database would have failed on the bad column.

A team has 300 end-to-end tests and 20 unit tests. The suite takes two hours and fails at random. What would you change first?

Its pyramid is upside down. Move logic checks into unit tests and boundary checks into integration tests, keeping a few end-to-end tests for key journeys, because lower levels are faster, steadier and say where a bug is.