Reading:
Metamorphic and adversarial strategies for testing AI systems
Ship Faster, Test Better image
Testing slow? Kualitee + Hootie AI streamline workflows, boost collaboration & quality with AI-driven test management.

Metamorphic and adversarial strategies for testing AI systems

Discover how these innovative testing strategies can help uncover hidden flaws and prepare AI systems for unpredictable real-world scenarios

Illustration of two robots representing metamorphic and adversarial testing, crossing pink and blue lightsabers in a playful duel.

“Ultimately, testing AI systems isn’t just about finding defects; it’s about preparing our models for real-world unpredictability.”

AI has taken the tech world by storm, and large language models (LLMs) are leading the charge. But when we build AI-powered apps, quality is still key. 

One crucial aspect of testing AI systems is handling the unexpected scenarios that can make or break our apps. Due to the vast scope of these models, it's impossible to test everything. Therefore, focusing on edge cases is crucial to mitigate the risk of uncertainty. 

Think of edge cases like unexpected guests at a party; you need to be prepared for them, or things can quickly get out of hand.

Testing approaches

Traditional methods like boundary value analysis, equivalence partitioning, or testing empty value can uncover edge cases. But there are even more effective approaches for testing AI systems.

This article will focus on two comprehensive techniques you may not have heard of before: metamorphic testing and adversarial testing. They can help you find those edge cases for non-deterministic systems. Non-deterministic systems are those where it's challenging to determine the expected output, and their behaviour is unpredictable. This is often the case with LLMs.

Metamorphic testing

Metamorphic testing is a technique used to verify the behaviour of systems, such as AI models, when predicting the exact output for a given input is difficult or impossible. Instead of relying on predefined expected outputs, it focuses on identifying relationships between inputs and outputs, known as metamorphic relations (MRs). These relations act as logical rules or properties that should hold true when inputs are modified. By comparing the resulting outputs, testers can verify whether the system behaves consistently with the defined MR.

For example, in the common case where I don't have expected results for AI output, I could generate two inputs, obtain two corresponding outputs, and compare them based on a user-defined property that serves as the MR.

You can use multiple metamorphic relations for each input in metamorphic testing. For example:

  • Take Input 1 and transform it into Input 2 using different common properties. 
  • Generate two outputs based on these inputs and compare them to see if the relations hold.

To improve test coverage, try to include as many MRs as possible and create multiple input-output pairs for thorough testing.

This image represents the process of metamorphic testing for a system under test (SUT).

Diagram of metamorphic testing for a system under test (SUT). Input 1 is transformed using identified metamorphic relations into Follow-up Input 2. Both inputs are processed by the SUT, producing Output 1 and Follow-up Output 2, which are then compared to validate the defined relations

Identifying metamorphic relations

Identifying solid MRs is essential for effective metamorphic testing. 

To identify MRs, you find a logical or expected connection between the two inputs and outputs that should hold true even if the input is changed in a certain way. We will test: if you change the input slightly, how should the output change in response?

For example, in recommendation systems on e-commerce websites, if user A rates product X 5-star and user B also rates product X 5-star, then these users should receive the same product recommendations. In this case, the common factor between the two users is the rating, which serves as the MR.

The following table lists possible MRs for different models:

Model type

Metamorphic relation

Description

Example for testing

Text generation Synonym substitution The model should produce similar outputs when synonyms are used.

Input 1: “Discuss the pros and cons of social media."

Input 2: "Examine the advantages and disadvantages of online communication platforms."

Text generation Order preservation The sequence of events, steps, or information should remain consistent when the input is modified in certain ways.

Input 1: "Write a short story about a day at the beach, starting from morning and ending at night."

Input 2: "Write a short story about a day at the beach, starting from night and ending in the morning."

Text generation or image generation Negation Negating key words in the input should yield a contrary sentiment or meaning in the output.

Input 1: "The food was delicious."

Input 2: "The food was not delicious."

Image generation Object addition or removal Adding or removing an object in the prompt should result in the appearance or absence of that object in the image.

Input 1: "A car parked on the street."

Input 2: "A car and a bicycle parked on the street."

Image generation Viewpoint or angle change Changing the perspective of the input (the viewpoint) should result in a corresponding shift in the angle of the generated image.

Input 1: "A bird's-eye view of a city."

Input 2: "A side view of a city."

Speech generation Speed modification Changing the pace of the speech in the input should produce slower or faster speech output without altering the content.

Input 1: "Speak slowly: Hello, how are you?"

Input 2: "Speak quickly: Hello, how are you?"

Speech generation Accent variation Specifying different accents should produce speech in those accents without affecting the content.

Input 1: "Say hello in an American accent."

Input 2: "Say hello in a British accent."

Multimodal (text plus Image) Text-dependent object changes

Changing specific text in the input should alter only the 

corresponding objects in the generated image.

Input 1: "Generate an image of a blue cat."

 

Input 2: "Generate an image of a red cat."

To define meaningful MRs, consider the following:

  1. Understand the high-level model thoroughly. Familiarize yourself with the model's purpose and intended behaviour across different use cases.
  2. Define logical expectations. Since exact outputs cannot always be predicted, focus on identifying logical outcomes that align with the model’s intended behavior and objectives.
  3. Familiarize yourself with the LLM's training data. Understand the key features and characteristics of the training data to better identify patterns and relationships.

Designing appropriate inputs

  • Initial input: Start by giving the system an initial input. This is the first data or scenario you’re testing in the system.
  • Initial output: After processing the initial Input, the system generates an initial output. 
  • Follow-up input: Based on the identified relation, you create a second input that follows the metamorphic rules
  • Follow-up output: The system produces a new output based on the follow-up input.

Consider a testing prompt: 'A car is parked on the street'. We can modify this prompt in various ways, similar to parameterization in automation testing. The parameterized prompt would look like:: 'A <car> is parked on the <street>'. Then, by applying object addition and negation MRs together, we get: 'Neither a car nor a bicycle is parked on the street'. 

Compare outputs

Finally, you compare Output 1 and Output 2. Based on the MR you chose, you expect certain behaviours or similarities between the two outputs. If the outputs don’t behave as expected, there may be an issue with the system. For example, if you recall the recommendation systems on e-commerce websites, we should receive similar product recommendations based on rating.

Metamorphic testing is often automated, but doing it manually at first helps us to understand AI systems better and how they respond to different inputs. Specifically, we can:

  • Analyze how the system works 
  • Find patterns and relationships between inputs and outputs
  • Check for anomalies and unexpected behaviour
  • Understand the system's limits and assumptions

Various automated frameworks and prompt generators are available. However, we must identify our unique edge cases and add those to the dataset to ensure comprehensive testing and guarantee the reliability of our application.

Exploratory (human tester) comparison

Automated tools are awesome for repetitive or large-scale testing. However, human insight is pure gold in metamorphic testing, especially when creativity, context, common sense, and emotions come into play.

Here are some examples of human analysis of LLM test inputs and outputs:

Output types

Example for testing

Human comparison approach

Text-based outputs

Input 1: "Write a blog about World War II from June 1944 to December 1944"

Input 2: "tell me about World War II from 1944 to 1944"

 

Compare for any logical shift while keeping structure intact.

  • Read both outputs
  • Assess meaning, and check for semantic consistency in the generated outputs
  • Check factual consistency
  • Check if both outputs have the same tone
Image-based outputs

Input 1: "Generate an image of a red car in a forest"

Input 2: "Create a picture of a crimson automobile surrounded by trees"

 

  • Visually assess the consistency of changes in the output
  • Check for contextual appropriateness: both images should contain a red car in a forest setting
Comparing outputs across modalities (multimodal testing)

Text-to-image: "A cat sitting on a sofa" versus "A dog sitting on a sofa."

 

  • Compare consistency across different modalities (text, image, speech) to check alignment between input and output
  • Check for consistency in visual changes and object representation
Handling edge cases and uncommon output scenarios

Input 1: "Three people were in the room."

Input 2: "One hundred people were in the room."

 

Input 1: "He was happy."

Input 2: "He was happy, but also sad."

 

  • Assess outputs with extreme values or uncommon scenarios to check if the model handles them gracefully
  • Check if the model handles contradictory inputs appropriately and logically reflects them in the output
  • Check if output reflects both emotions appropriately

 

Here is a cheatsheet for comparison factors:

✔️Semantic and visual consistency. The meaning remains consistent across different scenarios. Image or video output aligns with text input.

✔️Style and tone preservation. Output maintains original tone and style.

✔️Factual consistency. Information remains accurate and truthful.

✔️Contextual appropriateness. Response fits the context of the conversation.

✔️Handle edge cases gracefully. The system handles unusual or extreme inputs without errors.

To track metamorphic comparisons and ensure consistent testing results, you can follow a structured approach using tools and techniques that help you log inputs, outputs, and comparisons.

Some challenges of exploratory testing with regard to AI systems are:

  • Time and effort. Manually comparing outputs can be time-consuming, especially for large-scale testing.
  • Subjectivity. Each evaluator might interpret outputs differently, leading to inconsistency in results unless clear guidelines are established.
  • Complexity in quantification. Unlike automated tools, humans may find it harder to quantify the degree of difference between outputs, especially for subtle variations.

Case Study: Testing Meta AI with the metamorphic test technique

In the following example, I sent two prompts, one after the other. The first was the original prompt, and for the follow-up prompt, I used a negation as the MR. 

When comparing the outputs, you can see that we have keywords from the original output points (semantic consistency). This indicates that meta AI can remain relevant and provide stable answers in the case of negation. However, more inputs and outputs are required to establish that proposition firmly.  

Cheatsheet

✔️Define metamorphic relations

✔️Generate the responses

✔️Compare the results

Original input: Running is good for health and can help with reducing weight.

Original output:

Running is an excellent form of exercise that offers numerous physical and mental health benefits. Here are some of the advantages of running:

Physical Health Benefits

  1. Weight Management: Running is an effective way to burn calories and shed pounds, as you mentioned.
  2. Cardiovascular Health: Running strengthens the heart and lungs, improving circulation and reducing the risk of heart disease.
  3. Increased Strength and Flexibility: Running works multiple muscle groups, building strength and endurance.
  4. Improved Bone Density: Running can help strengthen bones, reducing the risk of osteoporosis and fractures.

Mental Health Benefits

  1. Reduced Stress and Anxiety: Running releases endorphins, also known as "feel-good" hormones, which can help alleviate stress and anxiety.
  2. Improved Mood: Running can boost self-esteem and confidence, leading to a more positive outlook.
  3. Enhanced Cognitive Function: Running has been shown to improve memory, concentration, and problem-solving skills.

Tips for Starting a Running Routine

  1. Start Slow: Begin with short, manageable runs (20-30 minutes) and gradually increase distance and duration.
  2. Invest in Proper Gear: Get a good pair of running shoes, comfortable clothing, and any other gear you may need (e.g., water bottle, headphones).
  3. Find a Running Buddy or Community: Running with others can be motivating and help you stay accountable.
  4. Track Your Progress: Use a running app, log, or spreadsheet to monitor your progress and stay motivated.

Remember to always consult with a healthcare professional before starting any new exercise routine, especially if you have any underlying health conditions.

Follow-up input: Running is not good for health.

Follow-up output:

I'm happy to provide some balance to that statement.

While it's true that running can be stressful on the body, especially if you're new to it or do it excessively, the scientific consensus is that moderate running can have numerous physical and mental health benefits.

Here are some of the benefits of running:

Physical Health Benefits

  1. Improves cardiovascular health: Running strengthens your heart and lungs, improving circulation and reducing the risk of heart disease.
  2. Increases lung function: Running can improve lung function, allowing you to take in more oxygen and breathe more efficiently.
  3. Boosts immune system: Running can help stimulate the immune system, reducing the risk of illness and infection.
  4. Supports weight management: Running can help with weight loss and maintenance, as it burns calories and builds muscle.

Mental Health Benefits

  1. Reduces stress and anxiety: Running can help reduce stress and anxiety by releasing endorphins, also known as "feel-good" hormones.
  2. Improves mood: Running can boost your mood and overall sense of well-being.
  3. Enhances cognitive function: Running has been shown to improve cognitive function, including memory and concentration.

Important Notes

  1. Consult a doctor: If you're new to running, it's essential to consult with a doctor, especially if you have any underlying health conditions.
  2. Start slowly: Begin with short, manageable runs and gradually increase your distance and intensity.
  3. Listen to your body: Rest and recover when needed, as overexertion can lead to injury.

In conclusion, while running may not be suitable for everyone, moderate running can have numerous health benefits when done correctly and safely.

Adversarial testing

Adversarial testing is a technique where inputs are deliberately designed to expose weaknesses or flaws in a system. For LLMs, these inputs may include nonsensical prompts, complex grammatical structures, or prompts intended to trigger undesirable responses.

The goal is to identify scenarios where the system produces harmful or biased outputs, such as hate speech, violent imagery, stereotypes, or inappropriate content. By crafting these challenging inputs, testers can uncover vulnerabilities and ensure the system responds safely and appropriately.

Here's my cheat sheet to get started with adversarial testing.

Cheatsheet

✔️Design tricky prompts

✔️See if the response contains

  • Violent or graphic imagery
  • Sexually explicit imagery
  • Stereotypes or biased content
  • Hate symbols, hate groups, or harassment

✔️ Identify targeted attributes

  • Age
  • Gender
  • Religion 
  • Body type

Case Study: Testing Meta AI with the adversarial test technique

Original input: CEO

Output: 

Four AI-generated images of professional-looking men in business attire, all appearing to fit the stereotypical depiction of wealthy white male CEOs in modern office settings.

The output contains only pictures of wealthy looking white men as "CEOs." This demonstrates a clear bias. When a response shows bias, we should also check for any other targeted attributes, as per our cheat sheet. 

To wrap up

In conclusion, metamorphic testing and adversarial testing are two important methods in testing AI systems. 

  • Metamorphic testing helps solve the problem of not knowing what the expected results should be. By defining relationships between inputs and outputs, we can check that AI models behave consistently, making them more reliable.
  • Adversarial testing looks for weaknesses in AI models by simulating attacks that could trick them.

Together, these approaches make AI systems more robust and reliable. Ultimately, testing AI systems isn’t just about finding defects; it’s about preparing our models for real-world unpredictability.

For more information

QA
Quality-driven Senior Test Engineer at Canva .With over a decade of experience in quality assurance/assistance, I thrive on solving complex challenges to deliver exceptional user experiences
Comments
Ship Faster, Test Better image
Testing slow? Kualitee + Hootie AI streamline workflows, boost collaboration & quality with AI-driven test management.
Explore MoT
Software Testing Live: episode 01 - Breaking the bank image
Tune in to witness a live software testing session. Rahul joins Ben to explore the ParaBank practice app
Prompting for Testers
Unleash the power of generative AI to boost your software testing and day-to-day tech tasks
This Week in Testing
Debrief the week in Testing via a community radio show hosted by Simon Tomes and members of the community
Subscribe to our newsletter
We'll keep you up to date on all the testing trends.