skip to content

general code and the edge-case tail · lesson 7 of 9 · sources checked 2026-11-30

tests with no expected page

This lesson tests general code where no stored page gives an expected record, and measures how strong those tests are.

status of this lesson: from a public source

purpose

A stored page with an expected record exists only for the pages that somebody kept. An input of a new page form to the general code has no such page. You must still find the faults in the shared functions that many targets call, and you must know if your tests can find them.

objectives

  1. Write metamorphic relations for an extractor: added whitespace, a new order of element attributes, and a wrapper element.
  2. Write a property test with generated inputs for a shared function.
  3. Calculate a mutation score and explain what it does not show.
  4. Select the code changes for a mutation test from the changed lines.

new terms

mutation score
The share of deliberate code changes that a test suite catches.

how it works

A regression corpus tests the code against stored pages with expected records. Each new page form is outside that set. For such a page, three kinds of test work with no expected record:

testwhat it compareswhat it finds
Metamorphic testThe records of two related inputsA change that alters a record when it must not
Property testEach output with a rule that holds for each inputAn output that breaks the rule
Mutation testThe tests with a changed copy of the codeA test suite that does not see a fault

metamorphic relations

In 2018, a review of metamorphic testing stated that the method both makes test cases and checks their results. Its central part is a set of metamorphic relations: necessary properties of the function, in relation to more than one input and their outputs ACM Computing Surveys, source 1.

A metamorphic relation for an extractor says which change of the page must not change the record. I use three relations:

  1. Add a whitespace node between two elements.
  2. Write the element attributes of one element in a different order.
  3. Put the product element inside one more wrapper element.

For example, a page gives the price "19.90" in a span with two element attributes. The page with the element attributes in the other order must give the same price. If it gives no price, the selector depends on the order of the element attributes. In the same way, a fail of relation 3 shows that the selector depends on the depth of the elements.

The relation uses no expected record. It uses only the record of the first page. Thus it tests each page form that the team has, also a new one.

property tests

A property is a rule that holds for each input of a class. A generator makes many inputs of that class, and the test checks the rule on each output.

For example, a shared function reads a price text and gives a number. Two properties of that function are:

  • The output is never negative.
  • When you format the output again and read it again, you get the same number.

A property test finds only the rules that a person writes. It does not find a wrong value that agrees with each rule.

mutation score

A mutant is a copy of the code with one small change on purpose. For example, a > becomes >=, or a branch returns early. A test suite kills a mutant when one or more tests fail on it.

The mutation score is the count of killed mutants divided by the count of all mutants. For example, a suite kills 26 of 40 mutants. The score is 26 divided by 40, thus 65%.

Some mutants do not change the behavior of the code. These are equivalent mutants, and no test can kill them. A runner that does not remove them gives a score that is lower than the true score.

A low score on a shared function is a strong cue. Each target that calls the function can get an edge case that no test sees. Then a fix for one target can be a regression for another target.

mutants of the changed lines only

A full mutation run on a large code base is slow. Thus I make mutants only on the lines that a change touches.

In 2018, Petrovic and Ivankovic described a diff-based approach in code review at Google. It made mutants from the changed lines and skipped lines with no statement coverage. It also skipped lines that they judged not of interest, which they called "arid" lines 40th International Conference on Software Engineering (ICSE), SEIP track, source 2.

For example, a change edits the price function and one comment. The run makes mutants of the price function only. A comment line gives no mutant.

the two sides

automation sidedetection side
A metamorphic relation tests an extractor where no expected record exists. A mutation score shows which shared functions have weak tests.The same relations test a detection feature: a change that does not alter the client must not alter the verdict.

design decisions

optiongaincostwhen it is correct
A stored page with an expected record for each testThe test gives the exact correct value.Each new page form has no test until somebody stores it.For the regression corpus
Metamorphic relationsEach page form gets a test, also a new form.A wrong record on both pages passes.For each extractor that reads page structure
Property tests with generated inputsMany inputs for one written ruleIt finds only the rules that a person writes.For shared functions with a clear rule, for example a price reader
A full mutation runIt measures each test of the code base.It is slow on a large code base.Before a large change of a shared function
Mutants of the changed lines onlyThe run is short enough for each change.Old weak tests outside the change stay hidden.For each change in review

diagram

metamorphic relations on an extractor input

The three changes of the page must give the record of the first page.source: My model of the metamorphic relations of an extractor

code example

The example tests a small synthetic extractor with the three relations of the diagram and with one property. It uses the standard library only, and the pages are synthetic.

m53-x12-relations.tstypescript

Metamorphic tests and property tests for a synthetic extractor: added whitespace, a new order of element attributes, and a wrapper element.

// Metamorphic relations and a property test for a synthetic extractor. All pages are synthetic.
// Synthetic extractor: reads the text of the span with the class "price".
function extract(page: string): { price: string | null } {
  let inside = false;
  let price: string | null = null;
  for (const m of page.matchAll(/<(\/?)([a-z]+)([^>]*)>|([^<]+)/g)) {
    const [, close, tag, attrs, text] = m;
    if (text !== undefined) { if (inside && text.trim()) price = text.trim(); }
    else if (tag === "span") inside = close === "" && /(^|\s)class="price"/.test(attrs);
  }
  return { price };
}

// Shared function: a price text to a number of cents.
function toCents(text: string): number {
  const [whole, part = ""] = text.replace(",", ".").split(".");
  return Number(whole) * 100 + Number((part + "00").slice(0, 2));
}

// Own seeded source (mulberry32), so that each run makes the same inputs.
function seeded(seed: number): () => number {
  let a = seed >>> 0;
  return () => {
    a = (a + 0x6d2b79f5) >>> 0;
    let t = a;
    t = Math.imul(t ^ (t >>> 15), t | 1);
    t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}

const BASE = '<div id="p"><span class="price" data-x="1">19.90</span></div>';

const RELATIONS: Record<string, (p: string) => string> = {
  "added whitespace": (p) => p.replaceAll("><", ">\n  <"),
  "new attribute order": (p) => p.replace('class="price" data-x="1"', 'data-x="1" class="price"'),
  "wrapper element": (p) => "<section>" + p + "</section>",
};

const assert = (ok: boolean, name: string): void => { if (!ok) throw new Error(`fail: ${name}`); };

function testRelations(): void {
  const first = JSON.stringify(extract(BASE));
  for (const [name, change] of Object.entries(RELATIONS)) assert(JSON.stringify(extract(change(BASE))) === first, name);
}

function testProperty(count = 500, seed = 7): void {
  const rnd = seeded(seed);
  for (let i = 0; i < count; i++) {
    const cents = Math.floor(rnd() * 10_000_001);
    const text = `${Math.floor(cents / 100)}.${String(cents % 100).padStart(2, "0")}`;
    assert(toCents(text) === cents, text);
    assert(toCents(text) >= 0, text);
  }
}

testRelations();
testProperty();
console.log("relations and property: pass");

output

One line when the three relations and the property hold.

relations and property: pass

The output shows that the three changed pages give the record of the first page, and that the 500 generated prices keep the two properties.

data

mutation score of shared functions and adapter code, before and after new tests

status of this figure: measured by me

mutation score of shared functions and adapter code, before and after new testsshared functions1: 33.09%2: 52%adapter code1: 42.4%2: 81.6%unit: %1. before the new tests2. after the new tests

lines: interval, level 0.95

source: Lab result of this course: own mutation runner on lab extraction code, 400 mutants; date 2026-11-13; condition: The runner does not find equivalent mutants, and its operator set is small. Teaching values for one host, not values for a real service.

data table
mutation score of shared functions and adapter code, before and after new tests
seriescode group%interval
before the new testsshared functions33.0927.8 to 38.85 (level 0.95)
before the new testsadapter code42.434.09 to 51.16 (level 0.95)
after the new testsshared functions5246.11 to 57.84 (level 0.95)
after the new testsadapter code81.673.9 to 87.41 (level 0.95)
An own mutation runner made 400 mutants of the lab extraction code. The score is the share of mutants that a suite kills. Intervals are Wilson 95%.

The chart is a lab result of this course on lab code. After the new tests, the score of both code groups goes up. The shared functions stay lower than the adapter code. Thus the shared functions are the place for the next tests.

The runner does not remove equivalent mutants, so each true score is higher by an unknown quantity.

failure modes

problemcausecorrection
A new page form breaks the extractor, and each test passes.Each test compares with a stored expected record, and the new form has none.Add metamorphic relations that start from the record of the new page.
A relation passes, but the record is wrong.The extractor gives the same wrong value on both pages.Keep the regression corpus for the exact values. A relation finds only a change.
A property test passes on a wrong output.The output agrees with each written rule.Add a rule, or add a stored case with the expected value.
The mutation score of a shared function stays low.Its tests do not fail when its code changes.Add tests for that function first, and measure the score again.
The mutation score looks low for no fault.The runner counts equivalent mutants.Examine the live mutants, and mark each equivalent mutant.
The mutation run takes too long for each change.It makes mutants of the full code base.Make mutants only on the changed lines that the tests cover.

check questions

  1. An extractor gives the price 19.90 for a page. The same page with one more wrapper element gives no price. What does the fail show?

    1. The stored expected record is wrong.
    2. The selector depends on the exact depth of the elements.
    3. The page has a new price.
    answer

    b. The selector depends on the exact depth of the elements.

    A wrapper element must not change the record. When the price goes away, the selector depends on the structure around the price, not only on the price element.

  2. Give one property for a shared function that reads a price text and gives a number, and say what a property test cannot find.

    sample answer

    One property is that the output is never negative. A second property is that the formatted output, read again, gives the same number. A property test does not find a wrong value that agrees with each written rule.

  3. A suite kills 26 of 40 mutants. Calculate the mutation score, and explain why the true score can be higher.

    sample answer

    The score is 26 divided by 40, thus 65%. Some mutants can be equivalent mutants that do not change the behavior of the code. No test can kill them. When the runner does not remove them, the true score is higher.

  4. A change edits a price function and one comment line. Which lines get mutants in a run on the changed lines?

    1. Each line of the code base
    2. The comment line only
    3. The changed lines of the price function
    answer

    c. The changed lines of the price function

    A run on the changed lines makes mutants only where the change touches code that tests cover. A comment line gives no mutant.

exercise

exercise Do this task on paper. The input is a synthetic extractor result for one page and three changed copies, and a synthetic mutation run.

Extractor results:

pagechangeprice in the record
Anone (the first page)19.90
Badded whitespace node19.90
Cnew order of element attributesno value
Dwrapper element19.90

Mutation run of the shared price function:

  • Mutants: 30.
  • Killed mutants: 18.
  • Equivalent mutants: 6. No test can kill them.
  1. Find the relation that fails.
  2. Write the part of the selector that the fail shows.
  3. Calculate the mutation score of the run.
  4. Calculate the score when you remove the equivalent mutants.
sample answer

The answer uses the synthetic input of the task.

Page C fails the relation of the element attribute order, because its record has no price. Thus the selector depends on the order of the element attributes, not only on their values.

The mutation score is 18 divided by 30, thus 60%. Without the 6 equivalent mutants, 24 mutants stay. The score is then 18 divided by 24, thus 75%.

sources

  1. Metamorphic Testing: A Review of Challenges and Opportunities, ACM Computing Surveys, paper, read 2026-11-22
  2. State of Mutation Testing at Google, 40th International Conference on Software Engineering (ICSE), SEIP track, paper, read 2026-11-22
  3. My model of the three test kinds, of the extractor relations, and of the mutation score, 2026-11 (not public).