Definition
An integration test is a test that runs two or more real components of a system together and checks that they work correctly across the interfaces between them. An interface carries data, calls, error reports and responsibility for shared resources such as memory buffers. The test checks that both sides of each interface understand these the same way. Components outside the tested group are replaced by stand-ins. An integration test sits between a unit test §10.1, which runs one component alone, and a system test §10.3, which runs the complete system.
The idea
Two components can each be correct by their own reading of an interface while the pair is wrong. In 1999 NASA (the National Aeronautics and Space Administration of the United States) lost the Mars Climate Orbiter. Ground software reported the effect of small thruster firings in pound-force seconds. The navigation software, as the interface specification required, read the numbers as newton-seconds. One pound-force second is about 4.45 newton-seconds, so the effect of every firing was underestimated by that factor, and the probe approached Mars far lower than planned. Each program did consistent arithmetic in its own units. The error existed only in the combination, where the output of one became the input of the other.
Interfaces inside one device go wrong in the same way. Two components can disagree about:
units, such as milliseconds versus timer ticks;
byte order §4.21, meaning which byte of a multi-byte number comes first;
whether a length counts a header or only the data after it;
ownership of a buffer, meaning which side must free it and when;
completion, meaning whether the work is done when a call returns, or later, when a callback §14.22 (a function the caller handed over to be called back) runs;
the meaning of an error code, for example whether 0 means success.
An integration test connects the real components and pushes real operations across the interface, so that such a disagreement becomes a wrong result the test can observe.
How it works
Name the interface and the claim. State which interaction the test is for, for example “a count decoded by the parser is stored by the storage component with the same value”.
List the participants. Follow the operation from the test’s input to the effect the test will observe, and list every component on that path. Mark each one as real, meaning the production code built the way the product is built, or as a substitute. A substitute is either a test double §10.35, such as a stub that returns fixed answers or a fake that is a simplified working version, or a test driver, which is code that calls the components in place of their real caller.
Choose an order of integration. Big-bang integration connects everything at once, and a failure is then hard to locate. Incremental integration adds one component at a time. Top-down integration starts from the highest caller, replaces the components below it with stubs, and swaps in the real ones step by step. Bottom-up integration starts from the lowest components and drives them with test drivers.
Drive an operation across the interface and observe its effect on the far side: the stored value, the returned error, the state of shared resources. Include failure paths: make the lower component fail, and check that the upper component reports the failure and frees what it should.
Record the participants with the result, because the list decides what a pass means.
A related practice keeps substitutes honest. A contract test is one set of checks that is run both against a real component and against the fake that replaces it in other tests. If the two ever behave differently on those checks, one of the two runs fails, which shows that the fake no longer matches the real component.
Worked example
A message carries a 2-byte count in big-endian order, most significant byte first. A parser P extracts the two bytes and passes them to a storage component S through the call s_store_count(b), where b is an array §14.5 of 2 bytes. The author of P meant the bytes to stay in message order. The author of S expected them in the little-endian order of the device’s processor, least significant byte first. Below S, the flash memory §6.6 is replaced by a fake that keeps the data in RAM.
The count is 258. Since , the count in hexadecimal is 0x0102, and the message carries the bytes 0x01, 0x02 in that order.
The unit test of P feeds P a message with count 258 and checks that P hands S the bytes
0x01,0x02in that order. This test passes.The unit test of S builds its own input in little-endian order,
0x02,0x01, calls S, and checks that S stores 258. S takes the first byte as the low part and the second as the high part: . This test passes too.The integration test runs the real P and the real S on a message with count 258. S receives
0x01,0x02and computes . Reading the count back from S gives 513, not 258, so the test fails.
The integration test found a disagreement that two passing unit tests could not find, because each unit test encoded its own author’s reading of the interface. Changing one participant changes what the test means. If S is replaced by a stub that returns success without storing anything, the same integration test passes, and the mismatch stays hidden. With the real S and fake flash, the test checks the interface between parser and storage, but it says nothing about the erase time of the real flash or about what happens when power fails during a write.
Stated precisely
Let the operation of the test pass, on its way from the input to the effect the test observes, through components . Let be the set of those components that are real and the set that are substitutes. The test exercises the interface between two components only if both are in . An interface between a real component and a substitute is checked only against the substitute’s model of the missing side.
A pass establishes the following: for this input and this starting state, the components in , connected to each other and to the substitutes in , produced an outcome that the oracle accepted. The result carries over to the system in which every substitute is replaced by its real component only under the assumption that each substitute behaved, on the calls this run made, as the real component would have. The number of real components is not a measure of quality. What matters is whether the components on which the claim depends are real: replacing the one component whose behavior the claim is about removes the claim.
Limits and common mistakes
Passing integration tests for each pair of components does not show that the whole composition works. Three components can work in pairs and still compete for one buffer pool, one time budget or one lock when all three run together.
Calling two components one after the other on one thread does not test their asynchronous interaction. A callback that arrives while another operation is in progress, or an interrupt §6.11 (a hardware signal that suspends the running code) between two steps, is not exercised.
Integration tests often check only the success path. Make each real dependency fail, run out of memory or answer late, and check what the component above it does.
A failure in an integration test is harder to locate than a failure in a unit test, because the defect can sit in any participant or in the interface between them. Unit tests of the participants narrow it down.
The word integration in a test’s name or folder says nothing about what the test checks. The list of real and substituted participants does.
Real components bring real variation: thread scheduling and timing can differ from run to run. An integration test is therefore more likely than a unit test to be a nondeterministic test §10.38, one that can pass or fail on the same code.
Questions answered
Which real components and which substitutes participate?
The participants are all the components on the path from the test’s input to the effect the test checks, and the answer must list each one as real or as a substitute, together with what each substitute leaves out. A typical record for a test of a device’s storage path reads:
real: the message parser, the protocol logic and the storage component, built with the product’s build settings;
substitute: the radio, replaced by a test driver that hands messages to the protocol logic;
substitute: the flash memory, replaced by a fake in RAM that enforces erase before write but has no erase time and never loses power;
substitute: the scheduler, because the test runs every step on one thread.
The test checks only the interfaces whose two sides are both real, here parser to protocol logic and protocol logic to storage. Every behavior that a substitute leaves out remains untested: the timing of the real radio, the erase time of the real flash, recovery after a power cut, and steps that run at the same time on different threads. Those claims need further integration tests with the real parts, or a system test.
Related entries: Unit test (§10.1), System test (§10.3), Mock versus stub versus fake (§10.35), Endianness (§4.21), Ownership (§4.2).