10 min read

Better testing in the agentic era

Better testing in the agentic era

While writing another mountain of tests for Firebrick, I started wondering about their quality. At that moment I had 1031 test cases for my project, and I kept adding more. I had a strong feeling that it was not all that I expected it to be.

So I went looking through webpages and papers again to find answers to a few questions I had around testing in the agentic era. You know how that goes, my sandboxing and code review articles are full of references to all sorts of interesting stuff.

After exploring the internet and reading up in papers, here’s my summary of what’s going on and how I approach testing with agents. I want to have the discussion with you around the question: what makes a good test strategy when coding with an agent?

Four pillars of a valuable test

I think most of you will have some idea of what a good test looks like. Vladimir Khorikov wrote a nice set of criteria in his book Unit Testing that help establish a set of criteria for a good test:

  1. The test must protect against regressions.
  2. Code written in a test must be resilient against refactoring.
  3. Tests must provide fast feedback.
  4. The test code must be maintainable.

It’s a lovely short list of things that make a test great; however, when I give this set to my coding agent, it will not understand what to do. I think this is due to how broad and vague these statements are. So I broke these four pillars down into their elements to truly understand how to improve my coding agent.

Tests are there to protect against regressions

It's funny to see that the most important step here is to look at why you're testing in the first place. An agent is quite good at following specifications these days, so the initial tests and implementation almost always match the specification. But there are still two reasons why you want automated tests in your codebase:

  1. Implementation and tests will contain the same mistakes.
  2. Breakages will occur in later tasks you assign to the agent.

The coding agent writes tests and implementation with the same context window and attention vectors. It's likely implementation and test code will match the same patterns and contain the same mistakes. Sadly, your agent can't fix this. You could use two agents here, one for implementation and another for tests. Frankly: you'll still find the same type of bugs. It's better to review your tests instead.

Another interesting issue is that agents can only focus on so much at a time because the LLM's context window is limited. Typically, this means that it will focus on a single task. Because it does this, you’ll find that other code will break that was never touched by the agent.

Your test code must withstand refactoring

Good-quality tests can withstand refactoring of the implementation. But not all refactoring actions are equal. Refactoring the content of a method is quite different from restructuring modules. I found that it is vital to have a code architecture with deep modules/components that hide many details behind narrow interfaces. Otherwise, you have to constantly break component interfaces and reimplement tests in your application to add new functionality.

If you’re interested in the philosophy behind deep modules, I can recommend reading the book “A Philosophy of Software Design” by Johan Ousterhout. He explains why deep modules work so well and has a lot more to say about software architecture in general that is worth a read.

The deep module approach helps reduce the complexity of test code because in many cases you only want to test interactions with a component or module's interface. Some people , I'm looking at you Matt Pocock, made this into an artform with instructions like: "Write tests at the seams". Some skills made by Matt read like a good fairy tale. Tests written against a narrow interface with deep implementations are simpler.

I found that having many unit tests on a set of deep modules with narrow interfaces can cause issues. Because unit tests tend to focus on internal behavior. And these kinds of unit tests typically work better with smaller modules. Smaller modules increase the chance that you have to break the interface between modules more often. As with all things in software engineering, here’s a trade-off to make.

I recommend you write unit tests for these cases:

  1. Logic with many combinations: There are algorithms with a lot of different paths that you don’t want to cover with functional tests at the seam of a module. It makes sense to add tests for these cases.
  2. Invariants that must always hold: State machines and complex structures typically include rules that must always hold. It’s important to have unit tests to ensure the invariants hold.
  3. Edge cases that are hard to reach but carry significant risks: Some runtime scenarios, for example in security modules, pose significant risk and involve many edge cases. Adding additional unit tests helps to catch the problems.
  4. Provide fast feedback to the agent: Some unit tests can be useful for quick feedback on edits. Functional tests require a lot more setup and are slower to run. It’s nice to have a base set of checks for the agent after each edit.
  5. Regressions: As with all things in software, we still break stuff sometimes. Always include a test that provides a minimal reproduction. Unit tests are the fastest way to reproduce. You can throw away these tests after a while, though.

For all other cases, focus more on system tests that emphasize functionality. A good rule for your agent to follow is to write functional tests at the seam of modules. Let it write unit tests for the previously mentioned cases; a property-based testing approach can be helpful.

Make sure your tests are fast

Moving from primarily unit testing to system testing helps your agent, because it can produce a better match with the specs. However, this comes with a downside. Functional tests are slower to execute and more brittle.

We used to have a test pyramid that encouraged the use of unit tests because they are the cheapest to run. The trade-off here is that while unit tests are faster, they are difficult for an agent to maintain, as I mentioned earlier in this post. I found that unit tests can make reviewing code harder, too, because many can be affected when system behavior changes.

Focus on these three things in functional tests:

  1. Make sure your functional tests run fast.
  2. Be careful when you run the functional tests.
  3. Focus on functional behavior.

All three of these aspects are important, but I consider performance of the functional tests to be the top priority, because nobody wants to wait 20 minutes on their tests. It completely destroys their value. So let's look at the performance first.

Make functional tests faster

In modern development environments, there are more tools at your disposal to speed up tests. For example, you can run many of the external dependencies locally thanks to containerization and libraries like Testcontainers.

However, even with these local test tools, you still have to deal with databases and messaging products that are slow to start. I recommend starting Testcontainers at the start of the test run and leaving them running.

For databases, it’s good to know that Postgres allows you to clone a database. I typically will migrate the database at the start of the test run, and then clone the initial database for each test. This process is very fast on Postgres. Alternatively, you can reset the database in between tests.

It also helps to have your application running at the start of the run and to keep it running. In Quarkus, for example, you can configure your test lifecycle to keep the application container alive. The same can be done with Aspire in .NET.

The key is to make sure you have fakes for external dependencies that I can’t control. For example, API endpoints provided by third parties. You don’t want to deal with them.

Timing the functional tests

Still, functional tests, despite their value, are slow. So it’s important to make sure you time them right. The agent doesn’t have to run all tests when completing a task. I prefer my agent runs only those functional tests that are affected by the change we’re implementing.

The instruction to run only affected tests used to be impossible, because it can be quite hard to decide what is affected. But agents can explore your codebase with complicated grep statements and even use Language Servers to find dependencies and usages, so instructing your agent to run only affected tests is quite easy to do.

If you want a more rule-based detection of affected tests, some programming languages support test impact analysis. For example, there’s Cargo-affected (https://github.com/max-sixty/cargo-affected) that does this.

Whatever method you use to reduce the number of tests run on change, I recommend ensuring your CI (Continuous Integration) pipeline still runs all tests. It’s important that the agent can’t escape judgment.

Focusing on functional behavior

Ultimately, you want to make sure that if you spend energy on functional tests, they give you value. Make sure you focus on the functional behavior and leave out the edge cases and other confusing things.

When you review code, you want to look at the test code, so the code must be readable and tell you exactly what behavior the system should exhibit.

It’s been almost 15 years since my first feature file, but I still find that specification-by-example is a great way to build functional tests for software. If you are going to write functional tests, I recommend looking into using feature files and specification-by-example practices. Here are a few of the benefits that I noticed:

  1. The tests are human-readable, complete with examples.
  2. You can use feature files as part of your specification.
  3. Feature files can serve as documentation for users.

There’s one challenge remaining for me: coding agents aren’t super awesome at writing feature files. So I typically let my agent write up the first set of scenarios, and then I step in to provide high-quality examples and refine the scenarios as needed.

Writing maintainable tests

This brings me to my final point. Maintenance is important when you use any kind of coding agent. The first few days, weeks, maybe months, you’re going to be happy with what your agent produces. Ultimately, though, the quality will drift, and you need to step in to take action.

There are a few strategies that I like to employ for maintaining my tests:

  • Use mutation testing to measure test effectiveness.
  • Analyze the test suite when you find a bug.
  • Run a retrospective with your agent.

Apply these steps regularly, and you won't have a giant pile of stinking vibe-coded crap in your source control environment.

Let’s first look at a cool technique to find test weirdness: mutation testing.

Applying incremental mutation testing

Not many people I talked to over the past 6 months have tried this in their codebase: modify code to see if the test fails. Agents seem to do this a lot more often: write code, include a test, and then break the code to confirm the test is valid. Anthropic, specifically, has been doing this a lot lately.

I personally don’t trust my agent to do this consistently, because, well, statistics and neural networks. Instead, I like to run mutation tests against my code. But with a twist, because mutation tests are super slow.

A typical mutation test framework will run tests against your code, modify the code to introduce mutations (such as changing mathematical operators), and then verify whether your tests catch the mutations. This takes a bit of work, of course.

Personally, I like to run mutation tests on anything modified in the codebase. This way I can be sure that new behavior or changed behavior has decent quality tests.

There are several frameworks available: Stryker is a great framework for TypeScript and .NET, while PIT is excellent for Java. Rust has a great tool called cargo-mutants.

Analyze the test suite when you find a bug

Sometimes, code still breaks, and that’s okay. As long as you can fix it and deploy the fix quickly. However, when you start fixing a bug, I recommend reviewing your test suite as well. Because a bug that survives after you’ve applied all the practices means something’s off with your harness.

Maybe your agent missed a scenario. Or worse, your approach to choosing what gets a functional test and what should be a unit test is flawed. This happens, and it’s only bad if you don’t do anything about it.

When I need to fix a bug, I focus mainly on the immediate surroundings of the module that contains the bug and make sure we have the right quality checks in place for that module. You can ask any coding agent to analyze the tests and add missing scenarios.

Run a retrospective with your agent

Sometimes, while fixing a bug, I run into problematic patterns in the code. For example, I’ve seen cases where I kept missing the same type of test. This is where the retrospective can help.

With humans, you run a retrospective on a whiteboard every two weeks at the end of an iteration. Why not do the same with your agent? You can ask your coding agent to review past conversations and identify things it keeps failing at.

Letting the LLM analyze past conversations is a great way to find missing instructions and skills. I typically ask for the following:

  1. Find areas where we need to introduce new skills, agents or instructions.
  2. Locate skills, agents, and instructions that can use improvement.
  3. Find skills, agents, and instructions that no longer make sense.

Note that I’m not asking for new tests or other code-related things. I strongly believe you should only introduce additional tests when you find bugs in your code or when designing new behavior. More tests do not always equal better quality.

Drawing inspiration from this, I recommend asking your agent to analyze existing test cases and propose cases for deletion. Especially when you find that tests are flaky. Fix them, or delete them!

The way forward

Initially I simply refused to think that the test pyramid had to change with the use of coding agents. I think it still has value, but we're shifting the focus towards choosing functional testing over unit testing. But it all depends on your project.

My key takeaway here: Go back to your project, have a discussion with your team about your test strategy and update it with agentic coding in mind. You may find that things can be a lot cheaper.

I recommend giving this article to your agent so it can build a skill for you to clean up your test act. I did it and deleted a few hundred tests that were of no value. I also updated my agent’s TDD skill to force it into functional testing.

I hope you enjoyed reading this, and hopefully I helped your agent improve itself!