QA glossary
What is a test harness?
A test harness is the supporting code and setup that runs tests and reports their results. It starts the software under test, feeds it inputs, replaces parts the test should not depend on, and records what happened, so a test can run without a person driving it.
An example from QA practice
A payment service needs to be tested, but the real bank is not available to tests and charges real money. The team writes a harness: it starts the service, replaces the bank with a stub that answers "approved" or "declined" on demand, sends a set of payment requests and checks each response. The tests can now run in a build pipeline in seconds, and the declined case, which a real bank rarely produces, is tested every time.
What a harness usually contains
- A runner: the part that finds the tests, runs them and collects pass or fail.
- Stubs and mocks: stand-ins for services, databases or devices the test should not call for real.
- Test data: inputs and expected outputs, set up before each run and cleaned afterwards.
- Reporting: logs, screenshots and a result that a pipeline or a person can read.
A caution
A harness is software and can have bugs. A stub that always answers "approved" makes every test pass for the wrong reason, so the harness itself needs a check: break the code on purpose and confirm the tests go red.
Common questions
What is the difference between a test harness and a test framework?
The words overlap, and teams use them loosely. A framework is the library that gives a structure for writing tests, such as a runner and assertions. A harness is the broader setup around a particular system, including stubs, test data, configuration and reporting. A harness is often built on top of a framework.
What is the difference between a stub, a mock and a spy?
A stub returns fixed answers so the code under test can continue, such as a fixed exchange rate. A mock is told in advance which calls to expect and fails the test when they do not happen, for example when the wrong request is sent. A spy records the calls made to it so a test can inspect them afterwards.
When do you need to build a test harness?
When the thing you want to test cannot be run alone or cannot be driven by hand repeatedly. Typical cases are code that depends on external services, hardware or large amounts of data, and tests that must run unattended in a build pipeline. For a simple function, a test runner and a few inputs may be all you need.
How BetterQA does it
Flows is our browser test tool and it runs suites headless, so it can be the runner in a pipeline. Where a test needs a stand-in for an outside service, our engineers build the stubs as part of the suite and keep them in the same repository as the tests, so they are reviewed like any other code.
Related terms
- Flaky test
- Test coverage
- Test automation
- Mock and stub
What the structure around your tests does for you.