Home Knowledge Base Metamorphic testing

Metamorphic testing is a software testing technique that tests programs using input transformations and expected output relationships — instead of requiring a test oracle that knows the correct output for each input, metamorphic testing checks whether related inputs produce appropriately related outputs, based on metamorphic relations.

The Oracle Problem

Metamorphic Testing Solution

How Metamorphic Testing Works

1. Identify Metamorphic Relations: Determine properties that should hold for the program.

2. Generate Source Input: Create an initial test input.

3. Execute Program: Run program on source input, get source output.

4. Transform Input: Apply transformation to create follow-up input.

5. Execute Again: Run program on follow-up input, get follow-up output.

6. Check Relation: Verify that source and follow-up outputs satisfy the metamorphic relation.

7. Report Violation: If relation is violated, a bug is detected.

Example: Testing a Search Engine

# Metamorphic Relation: Adding a document containing the query
# should not decrease the number of results.

# Source test:
query = "machine learning"
results1 = search_engine.search(query)
count1 = len(results1)

# Follow-up test:
# Add a new document containing "machine learning"
search_engine.add_document("New ML paper about machine learning")
results2 = search_engine.search(query)
count2 = len(results2)

# Check metamorphic relation:
assert count2 >= count1, "Adding relevant document decreased results!"
# If this fails, bug detected!

Common Metamorphic Relations

Example: Testing a Sorting Function

def test_sort_metamorphic():
    # Source input:
    source = [5, 2, 8, 1, 9]
    source_output = sort(source)
    
    # MR1: Permutation invariance
    # Shuffling input shouldn't change sorted output
    follow_up1 = [1, 9, 2, 5, 8]  # Same elements, different order
    follow_up_output1 = sort(follow_up1)
    assert source_output == follow_up_output1
    
    # MR2: Adding element
    # Adding an element should result in sorted list containing that element
    follow_up2 = source + [3]
    follow_up_output2 = sort(follow_up2)
    assert 3 in follow_up_output2
    assert len(follow_up_output2) == len(source) + 1
    
    # MR3: Removing element
    # Removing an element should result in sorted list without that element
    follow_up3 = [x for x in source if x != 5]
    follow_up_output3 = sort(follow_up3)
    assert 5 not in follow_up_output3

Applications

Metamorphic Testing with LLMs

Benefits

Challenges

Evaluation

Metamorphic testing is a powerful technique for testing programs without test oracles — it enables testing of complex systems like machine learning models, search engines, and scientific simulations where determining correct output is difficult or impossible.

metamorphic testingsoftware testing

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.