Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 50 ยท Quality, Testing and Delivery

Testing Levels and Strategy

Learn the four testing levels (unit, integration, system, acceptance), the test pyramid, and black-box versus white-box testing, with a tiny runnable test runner.

Software that has not been tested is software whose behavior you are guessing. Testing means running a program with chosen inputs to see whether it behaves as expected, in order to find defects and to build confidence. Testing cannot prove that a program has no bugs (as Edsger Dijkstra put it, it can only show their presence), but good testing makes serious bugs rare and makes change safe.

Teams do not test only once, at the end. Different tests answer different questions, and they are arranged in levels from small pieces of code up to the whole product in the customer's hands. Choosing how many tests to write at each level is your test strategy. This lesson explains the levels, the test pyramid, and the difference between looking inside the code and treating it as a black box.

The real test frameworks (JUnit, pytest, Jest and others) are not available on this site. The examples are plain JavaScript stand-ins: a tiny test runner written in a few lines, and a small shop example tested at three levels. They show what a framework does inside, but they are simplified teaching code, not a replacement for a real tool.

The four levels

Four testing levels

Unit One function or class alone.
Integration Several parts working together.
System The whole product, end to end.
Acceptance Does it meet the customer's need?

Higher levels are slower and broader; lower levels are faster and more precise.

1 / 2

Unit tests check the smallest meaningful piece, usually a function or a class, in isolation. They run in milliseconds, need no database or network, and tell you exactly where a failure is. A function that applies a discount code is a good unit-test target.

Integration tests check that parts work together: your code with a database, a message queue, another service, or a few of your own classes as a group. Interfaces are where many defects hide: a field is named differently, a date format differs, an error is not handled. Integration tests are slower and need more setup, and they catch what unit tests cannot.

System tests exercise the complete, deployed system through its public interface (the user interface or the public API), in an environment that resembles production. They check end-to-end flows, performance, security and other non-functional qualities (see functional and non-functional requirements).

Acceptance tests answer "did we build the right thing?" They are written from the customer's point of view and decide whether the product can be accepted. They often use the Given / When / Then style from user stories and use cases. In the V-model they pair with requirements (see V-model and iterative models): each development phase has a testing level that checks it.

Two names you will meet: alpha testing (by the organization's own staff before release) and beta testing (by a limited group of real users); both are forms of acceptance testing. Regression testing is not a level: it means re-running existing tests after a change to make sure that nothing that worked has broken.

Black-box and white-box testing

  • Black-box testing treats the program as a box with inputs and outputs. You decide what to test from the specification, with no knowledge of the code. It finds missing or wrong behavior. Techniques such as equivalence classes and boundary values belong here (see test design techniques).
  • White-box testing uses knowledge of the code's structure to choose tests, for example to make sure that every branch of an if runs. It finds errors in logic and unreachable code. Coverage measurements belong here.
  • Grey-box testing mixes the two: you test through the public interface, but you use some knowledge of the internals (such as a database schema) to design cases.

Use both. Specification-based tests tell you whether the right things are tested; structure-based measures tell you what you forgot to exercise.

A tiny test runner

Every test framework does three things: it collects tests, runs each one while catching failures, and reports. You can write the essentials in a handful of lines. This stand-in registers tests with test(name, fn), uses a thrown error to signal failure, and prints a result for each test. One test is deliberately failing, so that you can see what a failure report looks like.

JS A tiny test runner

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last test fails, and that is useful information. The test says that a negative tax rate should be rejected or ignored, but the function happily returns 9. The test exposed a missing rule. Whether the right fix is to throw an error or to treat the rate as zero is a requirement decision, which is why tests and requirements belong together.

The structure of a good test is often described as arrange, act, assert: set up the data, call the code, check the result. Keep each test small, independent of the others, deterministic (the same result every time) and named after the behavior it checks ("empty cart costs nothing"), so that a failing name already tells you what broke.

The same idea at three levels

To see the levels side by side, the next stand-in tests a tiny shop. A pure function applyDiscount is tested alone (unit). A checkout object that uses a store is tested with a fake in-memory store (integration of two parts). A scenario written in the customer's language checks the whole purchase (acceptance-style). In real life the store would be a database and the acceptance test would drive the real application.

JS One shop, three test levels

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the fake store. A fake, stub or mock stands in for a slow or unavailable dependency (a database, a payment provider, a clock) so that a test is fast and repeatable. Overusing mocks is a risk, though: a test that mocks everything checks only that your mocks agree with your code.

The test pyramid

How many tests of each kind should you have? The test pyramid, popularized by Mike Cohn, is a guideline: have many unit tests at the base, fewer integration tests in the middle, and a few system or end-to-end tests at the top.

flowchart TB
    A["Few end-to-end tests<br/>slow, broad, fragile"] --> B["Some integration tests<br/>moderate speed and scope"]
    B --> C["Many unit tests<br/>fast, precise, cheap"]

The pyramid: the lower the level, the more tests, and the cheaper each one is to run and to maintain.

The shape follows cost and feedback. Unit tests run in seconds, so you can run thousands after every change and they point to the failing line. End-to-end tests drive a browser or a deployed system, so they take minutes, break because of unrelated changes (a button moved), and are hard to debug. Too many top-level tests with few unit tests is called the ice cream cone anti-pattern: the feedback is slow and expensive. Some teams prefer a "testing trophy" or "honeycomb" that emphasizes integration tests, especially for services where most defects are at interfaces. The principle is the same: push each check as low as it can reliably go, and keep a thin layer of end-to-end tests for the most important user journeys.

Planning a strategy

A practical test strategy answers:

  • What is the risk? Spend more testing effort where failure is costly (payments, safety, security) or likely (new, complex code).
  • Which level catches it cheapest? A rounding rule is a unit test; "the order total matches the payment" is integration; "a user can buy and receive an email" is system.
  • What runs when? Fast tests on every commit, slower ones on every merge or nightly (see version control, CI and delivery pipelines).
  • Who owns it? Developers write unit and most integration tests; testers add exploration and system tests; customers take part in acceptance.
  • What is automated? Anything repetitive and stable should be; exploratory testing, where a human looks for surprises, stays manual.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which level tests one function in isolation, with no database or network?

    Unit tests exercise the smallest piece of code on its own, which makes them fast and precise.
  2. 2. What is the main goal of acceptance testing?

    Acceptance tests are written from the customer's point of view and decide whether the product is the right thing.
  3. 3. Which description matches black-box testing?

    Black-box tests are derived from specified behavior; white-box tests use the code's structure.
  4. 4. According to the test pyramid, which is true?

    Lower-level tests are faster, cheaper and more precise, so they should be the most numerous; a few end-to-end tests cover key journeys.
  5. 5. Why is the 'ice cream cone' shape considered a problem?

    When most tests are end-to-end, the suite is slow and brittle, and failures are hard to diagnose.

Summary

  • Testing runs software with chosen inputs to find defects and build confidence; it cannot prove the absence of bugs.
  • The four levels are unit (one piece), integration (parts together), system (the whole product) and acceptance (does it satisfy the customer?); regression testing re-runs tests after change.
  • Black-box tests come from the specification, white-box tests from the code's structure; use both.
  • A test framework is essentially: collect tests, run each while catching failures, report. Good tests are small, independent and deterministic.
  • The test pyramid advises many fast unit tests, fewer integration tests and a few end-to-end tests; strategy follows risk and cost of feedback.

Next: Test Design Techniques.