AI in Testing

Giving every change, human or AI, a fast way to prove it works.

As AI speeds up writing code, testing becomes the main safety net. The faster code is produced, the more important it is that every change can be checked automatically and quickly, before a person spends time reviewing it.

AI helps on both sides: it can write and maintain tests, and it can use tests to check and fix its own work. The key is giving it a reliable feedback loop and making sure it can't weaken the checks that protect you.

Generating tests

AI can draft unit tests, edge cases and integration tests from code or from acceptance criteria. It is especially useful for legacy code with little coverage, where writing characterisation tests by hand is slow. Review generated tests as carefully as generated code: a test that asserts the wrong behaviour is worse than none.

Test-first bug fixes

For bugs, ask the agent to reproduce the problem as a failing test first, confirm it fails for the right reason, then fix the code without changing the test. A test that existed before the fix, and that the agent couldn't edit, is the best evidence the bug is really gone.

A feedback loop for every session

Give agents a way to check their own work: one command for the build, one for the tests, and for UI work a browser or screenshot tool to compare against a design. State "done" in checkable terms, such as "all tests in this module pass". The agent then iterates until the check passes, so what reaches a person has already passed it.

UI and visual testing

Self-healing UI tests use AI to cope with small changes in page structure, reducing brittle test maintenance. Visual comparison against approved designs catches layout problems that functional tests miss. Both work best alongside, not instead of, well-designed test IDs and stable components.

Testing AI features themselves

Features built on language models need evaluation sets: real inputs with expected outcomes, scored automatically and by people. Run them on every change to prompts, models or retrieval, and add every production failure as a new case. This is the AI-era equivalent of a regression suite.

Where AI testing pays off first

The quickest wins are usually raising coverage on critical but untested code, writing tests for bug fixes, and keeping brittle end-to-end tests working as the UI changes. Measure the effect on escaped defects and on time spent maintaining tests, and expand from there.

Keeping the suite fast

AI makes it easy to generate many tests, and a suite that grows without care becomes slow and flaky. Review generated tests for value, remove duplicates, and keep the fast tests that run on every change separate from slower suites that run less often.

How to put it into practice

  1. Make sure tests run locally with one command and finish quickly.
  2. Add a "verify before done" instruction to your repository context file.
  3. Adopt test-first for bug fixes made by agents.
  4. Block agents from editing test files during fix tasks.
  5. Use AI to raise coverage on the riskiest untested code first.
  6. Build evaluation sets for every AI feature you ship.

Common mistakes

  • Letting an agent "fix" a failing test by weakening it.
  • Accepting generated tests without reviewing what they assert.
  • Slow, flaky test suites that agents and people learn to ignore.
  • Shipping AI features with no evaluation set.

How to measure progress

  • First-pass CI success rate for agent-written changes.
  • Defects escaping to production.
  • Test suite run time and flaky-test rate.
  • Evaluation pass rate for AI features over time.

Go further