Metamorphic
Often you can’t say what the right answer is. What should a parcel of 3,217 grams cost? Only the price list knows, and the code is the price list. But you can still say how two answers relate: a heavier parcel never costs less. A metamorphic property changes the input in a way you understand, and checks that the answer changes as it should.
The example
Section titled “The example”A courier prices parcels by weight: a flat rate up to 1 kg, another up to 5 kg, then a rate per kilogram.
static int costInCents(int weightInGrams) { if (weightInGrams <= 1_000) { return 400; } if (weightInGrams <= 5_000) { return 900; } return 400 + 50 * (weightInGrams / 1_000);}You don’t need the price of any one parcel. Add weight to a parcel, and it shouldn’t get cheaper:
@Testvoid aHeavierParcelNeverCostsLess() { var weights = integers().atLeast(1).atMost(10_000); forAll(weights, weights) .check( (weightInGrams, extraGrams) -> { int lighter = Shipping.costInCents(weightInGrams); int heavier = Shipping.costInCents(weightInGrams + extraGrams);
var message = "the heavier parcel costs " + heavier; assertTrue(heavier >= lighter, message); });}When it fails
Section titled “When it fails”the heavier parcel costs 650 ==> expected: <true> but was: <false>
aHeavierParcelNeverCostsLess failed after 51 tries (seed 0x562255fd672f7917) shrunk counterexample: weightInGrams = 1001, extraGrams = 4000 pin it: .example(/* weightInGrams */ 1001, /* extraGrams */ 4000) rerun: PROPTASTIC_SEED=0x562255fd672f7917 ./gradlew test --tests 'MetamorphicTest.aHeavierParcelNeverCostsLess'A parcel of 1,001 grams costs 900 cents. Add 4,000 grams and it weighs 5,001, which costs 400 + 50 × 5 = 650. The rate per kilogram starts too low, so the parcels just over 5 kg are cheaper than those just under. proptastic shrank the parcel to the lightest that costs 900, and the extra weight to the least that tips it past 5 kg.
The message comes from the check’s own text, "the heavier parcel costs " + heavier. A plain
assertTrue would say only expected: <true> but was: <false>.
Reach for it when…
Section titled “Reach for it when…”- you can’t work out the expected answer, but you know how answers relate: search results, prices, rankings, recommendations, numerical code, machine learning models;
- the relation is something a domain expert would say out loud: more weight never costs less, a narrower search never finds more, shifting every value shifts the maximum by the same amount;
- you have no reference implementation to compare with.
It won’t catch…
Section titled “It won’t catch…”- Answers that are wrong but keep the relation. Prices ten times too high still rise with the weight. Pin a few known prices with an example-based test too.
- Relations you don’t state. This property says nothing about lighter parcels, or parcels of the same weight. Each relation is a property of its own.
In the literature
Section titled “In the literature”Metamorphic testing was introduced by T. Y. Chen and colleagues in 1998, as an answer to the oracle problem: what to test when you can’t say what the right output is. A relation such as “more weight never costs less” is a metamorphic relation. Further reading lists the first paper and two surveys, Segura et al., 2016 and Chen et al., 2018.