Skip to content
Tutorials Center
My learning

Topics

Lesson 49 of 60 ยท Automation Concepts and Process

Unit Testing Frameworks, Stubs and Drivers

Learn what unit testing frameworks provide (JUnit, TestNG, pytest, Jest), component testing, stubs vs drivers, and top-down vs bottom-up integration.

A unit test exercises one small piece of code, usually a function or a class, in isolation. Writing a first one by hand is easy; writing and running thousands, reporting their results, and sharing set-up code is not. That is the job of a unit testing framework. This lesson explains what frameworks give you, how isolation works through stubs and drivers, and how those ideas decide the order of integration testing.

It extends Test Levels, which introduced unit, integration, system and acceptance testing and showed stubs and drivers briefly.

What a unit testing framework gives you

Whatever the language, the major frameworks share the same building blocks (sometimes called the xUnit family, after the pioneering SUnit and JUnit):

  • Test discovery and running. The framework finds functions marked as tests (by naming convention or annotation) and runs them all.
  • Assertions. Readable checks such as assertEquals(expected, actual), assertThrows, and approximate comparisons for floating-point numbers.
  • Fixtures (set-up and tear-down). Code that runs before and after each test (or once per group) to prepare and clean the environment, so tests do not depend on one another.
  • Parameterized tests. The same test body runs with many data rows (very handy after equivalence partitioning and boundary analysis).
  • Test isolation and ordering. Each test must pass or fail on its own, in any order.
  • Reporting. Pass/fail counts, failure messages and stack traces, often as XML for CI servers.
  • Mocking support. Libraries (or built-in tools) to create fake collaborators.
  • Coverage and tooling integration. IDE buttons, command-line runners, coverage reports.

Typical frameworks:

Language Common frameworks
Java JUnit, TestNG
Python pytest, unittest
JavaScript / TypeScript Jest, Vitest, Mocha
C# xUnit, NUnit, MSTest
PHP PHPUnit, Pest
Go the built-in testing package
Ruby RSpec, Minitest

JUnit and TestNG are covered hands-on in the JUnit and TestNG tutorial, and Jest in its own tutorial. The vocabulary here applies to all of them.

The shape of a good unit test

Most developers follow Arrange, Act, Assert (AAA):

  1. Arrange: build the objects and data.
  2. Act: call the code under test once.
  3. Assert: check the result.

Test names should say what is checked and under which condition, for example rejects_negative_quantity. Keep each test small, fast (milliseconds), deterministic and independent.

A tiny framework from scratch

Seeing a framework's core in twenty lines makes it less magical. The stand-in below has test registration, set-up/tear-down hooks, assertions that throw, and a runner that reports.

JS Build a mini unit testing framework

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

No output.

Real frameworks add many more features, but the loop is the same: collect, run each test in a fresh context, catch failures, report.

Component testing

A component (or module) is a larger unit than a function: a class with its helpers, a service, a UI widget. Component testing tests it in isolation from the rest of the system, still with fake collaborators, but through its public interface and with more realistic behavior than a one-function unit test. Many teams use the words "unit test" and "component test" loosely; what matters is that the test is fast, isolated and focused.

Stubs, drivers and other test doubles

To test a unit alone, you replace what it talks to. Replacements are called test doubles:

  • A stub stands in for a lower-level piece that the code under test calls. It returns canned answers. Example: a PaymentGateway stub that always says "approved".
  • A driver stands in for a higher-level piece that calls the code under test. It sets up inputs, calls the unit and checks outputs. A unit test method is a driver in its simplest form.
  • A mock is a stub that also records how it was used, so the test can verify the interaction ("send was called once with this address").
  • A fake is a simplified working implementation, such as an in-memory database.
  • A spy wraps a real object and records calls.

The memory trick: a stub is below (it answers), a driver is above (it drives). Both exist because the real counterpart is not written yet, is slow, unreliable, expensive or has side effects (sending real emails).

flowchart TB
  DRV["Driver<br/>(replaces the caller)"] --> UUT["Unit under test"]
  UUT --> STB["Stub<br/>(replaces the dependency)"]

The driver pushes inputs in from above; the stub supplies answers from below.

Top-down and bottom-up integration

When parts are combined into larger groups, stubs and drivers decide the order.

  • Top-down integration: start from the top module (the main flow or UI) and add lower modules step by step. Missing lower modules are replaced by stubs. Advantage: early check of the main flow and design. Disadvantage: low-level modules are tested late, and many stubs are needed.
  • Bottom-up integration: start with the lowest-level modules and move upward. Missing higher-level modules are replaced by drivers. Advantage: foundation is tested early, no stubs. Disadvantage: the overall flow appears late.
  • Sandwich (hybrid): combine both and meet in the middle layer.
  • Big bang: wait until everything exists and connect it all at once. Quick to plan, but failures are hard to localize.
  • Incremental in general: add one module at a time, test, then continue, so faults are localized.
Approach Needs Strength Weakness
Top-down Stubs Early view of main flow Late testing of low-level logic
Bottom-up Drivers Solid base early Late view of main flow
Sandwich Both Balanced More coordination
Big bang Neither Simple plan Hard to find the failing part

Try it: a stub and a driver in action

Here the code under test, checkout, calls a payment service. We test it top-down using a stub for the unwritten PaymentGateway, and then test the lower level with a driver that calls it directly.

JS A stub below and a driver above

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

No output.

The stub lets the middle module be tested before the gateway exists and under conditions (such as "declined") that a real gateway would make awkward. The driver does the opposite for the lower module: it plays the role of the missing caller.

Good habits for unit tests

  • One behavior per test; the name tells what broke.
  • No dependence on the network, clock, random values or the file system unless that is the thing being tested. Replace them with doubles, or inject fixed values.
  • Do not over-mock: a test that is mostly mocks verifies the mocks. Prefer real objects when they are fast and simple.
  • Keep tests fast so they run on every save and every commit.
  • Treat test code with the same care as production code; see Keeping automation maintainable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the role of a fixture (set-up and tear-down) in a unit test framework?

    Fixtures create the same known starting state before each test and clean up after it, so tests do not affect each other.
  2. 2. Which one is a stub?

    A stub replaces a lower-level dependency that the code under test calls and returns canned answers.
  3. 3. Bottom-up integration testing needs which test double for the missing higher-level modules?

    In bottom-up integration, drivers call the lower-level modules that exist, standing in for the missing callers.
  4. 4. What does Arrange, Act, Assert describe?

    You set up the data, perform one action, and check the outcome.

Summary

  • Unit testing frameworks provide discovery, assertions, fixtures, parameterization, isolation, reporting and mocking support; the xUnit family shares one design.
  • Component tests run a larger module in isolation, usually with fake collaborators.
  • Stubs answer calls from the unit (below it); drivers call the unit (above it); mocks, fakes and spies are related doubles.
  • Top-down integration needs stubs, bottom-up needs drivers, sandwich needs both, and big-bang needs neither but localizes faults poorly.

Next up: Automation Frameworks: Data-Driven, Keyword-Driven, Hybrid, POM.