Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Writing tests

Writing tests is considered by many to be one of the most “boring” parts of software development. However, it is also one of the most important. Tests are the safety net that allows developers to make changes with confidence, ensuring that new code does not break existing functionality, and that the code does what it’s supposed to do.

Agentic AI has made it even more important to have a robust test suite, since the agent gets immediate feedback on whether its changes broke something or are not implemented correctly. This allows for an iterative development process where the agent can write code and then iterate on it until everything works well (make sure the testing commands are mentioned in the AGENTS.md). It also gives you peace of mind that the agent’s work is correct, especially if you are not painstakingly reviewing every line of code it writes.

Test-driven development

Test-driven development (TDD) is a software development approach where tests are written before the code. This approach holds value in the context of agentic AI, since it allows the agent to clearly understand the requirements of the code that is needed. A test is a more stringent way to specify how you want the code to behave than simply describing it in plain text. By manually writing a few simple tests, you explicitly enforce the interface and the behavior of the code. The agent will then be constrained to make these tests pass, and will be able to iterate on its implementation until it does.

However, we already mentioned that writing tests can be one of the most boring parts, and this approach ends up focusing most of our work exactly on this. Thankfully, agentic AI can help us here as well. After writing only a few simple tests, we can ask the agent to write more tests for us (see Repetitive work) or to make the tests we wrote more general or robust (for example with a property-based testing tool like Hypothesis).

Coverage-driven test writing

As we discussed, a good test suite is very useful for agentic AI. Such a suite covers most of the codebase, ideally all of it. Getting to 100% coverage is a tedious task, especially if you didn’t write tests as the code was being developed. Agentic AI can help with this as well, but you need to be careful. After all, 100% coverage does not automatically mean that the test suite is good!

Coverage-driven test writing is the process in which you use a coverage reporting tool to identify which parts of the codebase are not covered by the tests. An agent can then use this information to write new tests, or adapt existing ones, so that all of the missing parts are also covered. Generally, this is done as an iterative process, where the agent will write tests, produce a coverage report again, and repeat until the coverage is satisfactory.

This process can occur in two different contexts:

Improving the test suite

These are some ways to improve the test suite of a codebase.

Common pitfalls

Exercise

Improve the test coverage of agentic-ai-example and inspect which generated tests are worth keeping.