AI-Assisted Engineering

AI-Assisted Testing: Automatically Finding Bugs in Software

Writing tests is the part of software engineering that developers most often skip. Search-based tools, reinforcement learning and now large code models can generate tests automatically. We compare the approaches, show how to use them effectively, and explain why generated tests need their own quality check.

By EthicalHat Research · · 3 min read
Infographic: Approaches to automated test generation

Every engineering team agrees tests are important. Every engineering team also has code with too few of them. Writing good tests is time-consuming, and under deadline pressure it is often the first thing to slip.

Automatically generating tests has been a research goal for decades. In 2021, three strands of work are converging: mature search-based tools, commercial tools using reinforcement learning, and large code models like OpenAI's Codex that can write tests the way they write any other code.

Approach 1: search-based test generation

Tools such as EvoSuite (for Java) and Pynguin (for Python) treat test generation as a search problem. They generate sequences of method calls and inputs, run them, and evolve them towards goals such as higher branch coverage. Assertions capture the observed outputs.

Strengths: systematic, finds edge cases humans miss, always produces runnable tests. Weaknesses: tests can be hard to read, with odd inputs and many assertions, and they assert current behaviour, whether or not it is correct.

Approach 2: reinforcement learning

Commercial tools such as Diffblue Cover use reinforcement learning to write unit tests for Java code at scale, aiming for readable tests that compile and run. They are used to add tests to legacy codebases quickly, before refactoring or upgrading them.

Approach 3: large code models

Codex and Copilot can write tests from a function and a prompt, in the style of existing tests in the project:

python
# Existing function
def normalise_phone(number: str, default_country: str = "US") -> str:
    ...

# Prompting with the start of a test file often yields cases like these:
import pytest
from contacts.phone import normalise_phone

@pytest.mark.parametrize("raw,expected", [
    ("(415) 555-0132", "+14155550132"),
    ("+44 20 7946 0958", "+442079460958"),
    ("415.555.0132", "+14155550132"),
])
def test_normalises_common_formats(raw, expected):
    assert normalise_phone(raw) == expected

def test_rejects_too_short_numbers():
    with pytest.raises(ValueError):
        normalise_phone("555-01")

Strengths: readable, idiomatic tests with meaningful names and realistic scenarios, drawing on patterns from huge amounts of test code. Weaknesses: generated tests may not compile, may call functions incorrectly, or may assert wrong expected values. The model does not run them.

The fundamental limitation: the oracle problem

Every approach faces the same issue. A test needs to know what the correct output is (the "oracle"). Automated tools can observe what the code does, or guess what similar code usually does, but they cannot know what it is supposed to do.

That means generated tests often enshrine existing bugs: if a function returns the wrong value today, a test asserting that wrong value will happily pass.

Checking the tests: mutation testing

How do you know if generated tests are any good? Coverage alone is misleading: a test can execute every line without checking anything meaningful.

Mutation testing introduces small deliberate faults ("mutants") into the code, such as flipping a comparison or changing a constant, and checks whether the tests fail. Tests that let mutants survive are weak.

flowchart LR
  C[Code] --> M[Create mutants<br/>e.g. < becomes <=]
  M --> T[Run test suite<br/>against each mutant]
  T --> K{Tests fail?}
  K -->|yes| Killed[Mutant killed ✓]
  K -->|no| Survived[Mutant survived ✗<br/>tests miss this fault]

Tools such as PIT (Java) and mutmut (Python) make this practical, and they are an excellent companion to any test generation approach, human or automated.

Using AI-assisted testing well

  1. Use generated tests as characterisation tests for legacy code before refactoring: they capture current behaviour so you notice changes.
  2. Review expected values. For new code, a human must confirm assertions reflect intended behaviour, not just current behaviour.
  3. Run generated tests in CI immediately so non-compiling or flaky ones are caught.
  4. Measure with mutation testing, not just coverage.
  5. Combine approaches: search-based tools for systematic edge cases, code models for readable scenario tests, humans for intent.

Security testing too

The same ideas apply to security. Fuzzers generate inputs to find crashes and memory errors; ML is being explored to guide them. Code models can draft security-focused tests ("user A cannot read user B's order") that encode requirements scanners cannot infer.

The bottom line

AI-assisted testing can close coverage gaps quickly and make adding tests to neglected code far less painful. It cannot know what your software is meant to do. Pair generated tests with human review of expectations and mutation testing, and they become a powerful safety net rather than a false sense of security.

Our secure SDLC team uses AI-assisted test generation within a verified workflow, and our application security testing adds security-focused test cases on top.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.