Definition
Metamorphic testing is a testing method that checks a relation between the outputs of two or more runs of a program, instead of checking one output against a known correct value. The tester changes an input in a controlled way, called a transformation, and a metamorphic relation states how the new output must relate to the original one: it must stay equal, or change in a predicted way. If the relation follows from the specification, a pair of runs that violates it proves a fault.
The idea
Many programs have no practical test oracle §10.5, meaning no way to know the correct output for an arbitrary input. Take a program that computes the shortest route between two places on a road map. For a random map with thousands of roads, nobody can say by hand what the shortest distance is. Some facts about the output, however, follow from the meaning of the problem without computing it. On a map whose roads can be driven in both directions, the distance from place P to place Q equals the distance from Q to P. Making one road longer can never make the shortest distance shorter. Each fact relates two runs, and each pair of runs can be checked automatically on as many random maps as the tester likes.
T. Y. Chen, S. C. Cheung and S. M. Yiu proposed the method in a 1998 technical report [490]. The run on the original input is called the source test case, and the run on the transformed input the follow-up test case. The idea in one line: when the right answer is unknown, test whether the program’s answers agree with one another in the ways the specification requires.
How it works
Metamorphic testing proceeds in five steps.
Choose a transformation. Pick a change to the input whose effect on the correct output the specification determines: reorder items that the specification treats as unordered, add a known item, swap the two ends of a route.
Derive the relation. Write down how the two outputs must relate, such as equal, larger by a known amount, a subset, or never smaller, and the precondition under which that follows from the specification. A relation that only seems plausible is not enough: a relation that does not follow from the specification rejects correct programs.
Generate the source input at random, from existing tests, or both, and compute the follow-up input by applying the transformation.
Run both inputs from the same initial state, so that nothing left over from the first run affects the second.
Check the relation. Report a violation with both inputs and both outputs. A violation shows that at least one of the two outputs is wrong; it does not say which.
A round-trip check is a common relation between two programs that undo each other. Serialization §4.28 turns a value into bytes and deserialization §4.29 turns the bytes back into a value; a compiler can turn a policy written as text into a lower-level program, and a decompiler turn that program back into text. The relation requires that encoding and then decoding, or compiling and then decompiling, return the starting value, compared under a stated equality, for example equality after both sides are rewritten into one standard form.
Worked example
A function adds a list of integers. Its contract says that the true sum fits in the integer type, so no overflow occurs. Two relations follow from the meaning of addition.
Permutation. Reordering the list leaves the sum unchanged.
Appending a value. Appending a value increases the sum by exactly .
For the source list the correct sum is . The permuted list must give the same result, and the list must give .
Three faulty versions show what the two relations catch.
Version 1 skips the last element. It returns for and for . Since , the permutation relation fails.
Version 2 always returns 0. The permutation relation passes, since . The appending relation fails: the extended list gives 0, but the relation demands .
Version 3 returns the true sum plus 1. The permutation relation passes: . The appending relation also passes: the extended list gives , and the relation demands . Both relations hold, yet every output of version 3 is wrong.
Only a test with a known answer catches version 3: the empty list must give 0, or must give 9, computed by hand.
Floating-point numbers §4.49 break the permutation relation under exact equality. In the 64-bit floating-point format of IEEE 754 [271], which most programming languages use for numbers with fractions, gives 0.6000000000000001, while gives 0.6, because each addition rounds its result to the nearest number the format can represent. For floating-point sums the relation must allow a stated tolerance, or it rejects correct code.
Stated precisely
Let be the program under test, a source input that satisfies a precondition , and the transformation, so that is the follow-up input. A metamorphic relation is a condition on the two inputs and their two outputs. The relation is valid when every program that meets the specification satisfies In the sum example, appends , and is , under the precondition that the true sums of both lists fit in the integer type.
If is valid and the check fails for some , then is wrong on or on . This conclusion rests on three assumptions.
The relation is valid. This is a claim about the specification, and the tester must justify it.
The two runs do not influence each other.
The program is deterministic, giving the same output whenever it gets the same input, or the relation allows for the variation.
A relation that holds shows only that this pair of runs is consistent. Relations are necessary conditions on a correct program, not sufficient ones, as version 3 above shows.
A round-trip relation for an encoder and a decoder states for every value that the encoder accepts. The reverse relation, for every byte string that the decoder accepts, holds only when the format is canonical, meaning that each value has exactly one valid encoding. A format that allows both a short and a long form of a length field breaks the reverse relation without any bug.
Limits and common mistakes
A relation must be derived, not assumed. Reordering two messages of a network protocol is not harmless when each message carries a sequence number or an authentication §1.6 tag, a short value computed with a secret key that the receiver checks: the reordered messages are rightly rejected. Appending bytes to a message whose length or checksum §4.32 is checked turns a valid message into an invalid one.
Consistency is not correctness. A program that is wrong in the same way on both runs passes, as version 3 shows. Keep at least one known-answer test, whose expected output is worked out independently of the program.
A round trip hides errors that the encoder and the decoder share. A length of 258 sent in big-endian §4.21 byte order, most significant byte first, is the bytes 0x01 0x02. An encoder that writes 0x02 0x01 and a decoder that reads the bytes the same wrong way agree with each other on every value, yet both disagree with the protocol. Check at least one encoding against bytes taken from the specification.
A failed check names a pair of runs, not the faulty one.
The method tests a finite number of inputs and relations. It finds a fault only where some chosen relation constrains the output.
Floating-point arithmetic, concurrent execution and random choices inside the program can each break exact equality. The relation must state what variation it allows.
Questions answered
Which transformation should preserve or predictably change the result?
A transformation is fit for use only if the specification determines its effect on the correct result: it must leave the result unchanged, or change it by an amount that can be worked out without running the program. In metamorphic testing, the tester runs a program on an input and on a transformed copy of it and checks a stated relation between the two outputs. Transformations that preserve the result include reordering the terms of an integer sum that cannot overflow, swapping the two ends of a route on a map whose roads run both ways, and encoding a value and decoding it again. Transformations that change the result predictably include appending a known item to a sum, which increases the sum by exactly that item, and making one road longer, after which the shortest distance cannot shrink.
For each candidate, write down the precondition under which the relation follows from the specification, and reject transformations whose effect is only assumed to be harmless. Reordering the messages of a protocol that checks sequence numbers, appending bytes to a message with a checked length, and reordering floating-point additions all change the correct result, so an equality relation would reject correct programs. Keep at least one test with a known answer as well, because a program that is wrong in the same way on every run satisfies every relation.
Related entries: Test oracle (§10.5), Property-based testing (§10.7), Serialization (§4.28), Endianness (§4.21), Binary floating-point representation, special values, and rounding (§4.49), Differential testing (§10.6). Further reading: [271, 490].