Test Harness
What is a Test Harness?
A test harness is a collection of software and test data configured to test a program by running it under varying conditions and monitoring its behaviour and outputs.
Key Components of a Test Harness
- Test Execution Engine: Runs the test cases automatically.
- Stubs and Drivers: Simulate missing components that the code under test depends on.
- Test Data Sets: Predefined input data used to exercise the code.
- Result Comparison Logic: Compares actual outputs against expected outputs automatically.
Why Test Harnesses Matter
Test harnesses are especially important in automated and unit testing, enabling code to be tested in isolation before all its real dependencies are available, and allowing tests to be run repeatedly and consistently.
Stubs and Drivers in a Real Example
Suppose you are testing a checkout() function that calls a payment gateway. The real gateway is slow, costs money per call and may not even be available in the test environment. A harness replaces it with a stub and drives the function with prepared inputs.
# code under test
def checkout(cart_total, gateway):
if cart_total <= 0:
return "invalid"
response = gateway.charge(cart_total)
return "paid" if response["status"] == "success" else "failed"
# harness: test data + stub + driver + result comparison
import unittest
from unittest.mock import Mock
class CheckoutHarness(unittest.TestCase):
def setUp(self):
self.gateway = Mock() # stub for the real gateway
def test_successful_payment(self):
self.gateway.charge.return_value = {"status": "success"}
self.assertEqual(checkout(499, self.gateway), "paid")
def test_declined_payment(self):
self.gateway.charge.return_value = {"status": "declined"}
self.assertEqual(checkout(499, self.gateway), "failed")
def test_zero_total_never_calls_gateway(self):
self.assertEqual(checkout(0, self.gateway), "invalid")
self.gateway.charge.assert_not_called()
if __name__ == "__main__":
unittest.main() # execution engine
Here the stub stands in for a component the code calls (the gateway), while the driver is the test code that calls the unit under test because no real caller exists yet. In bottom-up integration you mostly need drivers; in top-down integration you mostly need stubs.
Harness vs Framework vs Tool
| Term | What it is | Example |
|---|---|---|
| Testing framework | A general library of rules and helpers for writing tests | JUnit, TestNG, pytest, unittest |
| Test harness | The specific setup that runs your tests against your code: framework plus stubs, data, configuration and reporting | The project's test folder with fixtures, mocks and a runner config |
| Test tool | Software that supports one activity in testing | Selenium for browser control, Postman for API calls |
In short, the framework is the toolkit and the harness is what you assemble with it for one system.
Building a Harness Step by Step
- Identify the unit or module to test and list everything it depends on: databases, APIs, files, the clock.
- Decide which dependencies to replace with stubs, mocks or fakes, and which to run for real.
- Prepare test data sets, keeping them in files or fixtures rather than scattered through test code.
- Write drivers or test functions that feed the data in and capture outputs.
- Add automatic comparison of actual and expected results, with clear failure messages.
- Hook the harness into the build pipeline so it runs on every change and publishes a report.
Pitfalls to Watch
- Stubs that drift from reality. If the real API changes its response format and the stub does not, tests pass while production fails. Back unit-level harnesses with a smaller set of real integration tests.
- Shared state between tests. One test leaving data behind can make another pass or fail randomly. Reset state in setup and teardown.
- Over-mocking. Mocking everything means you are testing the mocks, not the logic.
- Slow harnesses get skipped. Keep the main suite fast enough that developers run it before every commit.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.