Test Runner
In short
A test runner is a tool that finds a project's automated tests, executes them, and reports which ones passed or failed, usually from a single command.
What is a test runner?
A test runner is the program that actually runs your automated tests. You start it with one command, such as npm test, pytest, or go test, and it finds the tests, runs them, and prints a summary of what passed and what failed. It exits with a nonzero exit code when any test fails, which is how a CI/CD pipeline knows to stop a broken change.
A runner first discovers tests, usually by file name patterns such as *.test.js or test_*.py, or by annotations in the code. It then sets up fixtures, runs each test, often in parallel across several worker processes, captures output and timing, and produces a report: a console summary, or a machine-readable file such as JUnit XML that CI systems can display. Useful features include a watch mode that reruns tests when files change, filtering by name or tag, rerunning only failed tests, timeouts for tests that hang, and shuffling the test order to expose tests that depend on each other.
A test runner is like the referee and scoreboard at a tournament: it doesn't play the games, but it makes sure each one is played under the rules and records the results. Every major language ecosystem has at least one, and some are built into the language's toolchain, such as node --test in Node.js, go test in Go, and cargo test in Rust.
Test runners are often confused with testing frameworks and assertion libraries. The framework provides the API for writing tests, such as describe, test, and fixture hooks; the assertion library provides the checks, such as expect(x).toBe(y); and the runner executes the tests and reports results. Many popular tools bundle all three, which is why the names are often used interchangeably. A CI server is different again: it runs the test runner as one step of a larger pipeline.
Key takeaways
- A test runner discovers, executes, and reports on automated tests.
- A nonzero exit code on failure lets CI pipelines block broken changes.
- Common features include parallel runs, watch mode, filtering, and timeouts.
- Reports can be human-readable or machine-readable, such as JUnit XML.
- The framework defines how tests are written; the runner executes them.
Example
# Discover and run all test files with Node.js's built-in runner
node --test
# Run only the tests whose names match a pattern
node --test --test-name-pattern="checkout"
# Rerun affected tests automatically whenever a file changes
node --test --watch
# Python: run tests matching "checkout" and stop at the first failure
pytest -k checkout -x
# 0 means every test passed; anything else means at least one failed
echo $?Readers ask
What is the difference between a test runner and a testing framework?
A testing framework gives you the functions for writing tests and organizing them, while the test runner finds and executes those tests and reports the results. Many tools include both, so the terms are often used loosely.
Why do test runners run tests in parallel?
Running tests at the same time across several processes shortens the feedback loop, which matters for large suites. It only works reliably when tests are independent and don't share data they modify.
See also
- Unit TestTesting & Quality, p. 35A unit test is a small, automated check that verifies one function, method, or class behaves correctly in isolation from the rest of the program.
- Test FixtureTesting & Quality, p. 31A test fixture is the known state a test needs before it runs, such as sample data or a configured object, plus the code that sets it up and tears it down.
- AssertionTesting & Quality, p. 3An assertion is a statement in code declaring that a condition must be true at that point, stopping the test or program with an error if it is false.
- CI/CDDevOps & Cloud, p. 9CI/CD is a set of automated practices that build, test, and release code changes frequently, so software can be delivered to users quickly and safely.
- Flaky TestTesting & Quality, p. 10A flaky test is an automated test that sometimes passes and sometimes fails without any change to the code, which makes its results hard to trust.
- Test CoverageTesting & Quality, p. 30Test coverage is a metric that measures how much of a program's source code is executed when its automated tests run, usually shown as a percentage.
- JestTesting & Quality, p. 13Jest is a popular JavaScript testing framework that bundles a test runner, assertions, mocking, snapshots and code coverage in one package with little setup.
- PlaywrightTesting & Quality, p. 19Playwright is Microsoft's open-source framework for end-to-end testing and browser automation, driving Chromium, Firefox and WebKit through one API.
Spotted a mistake or something missing on this page?Suggest an edit