Integrating TDD and generative AI: another step forward in code quality

  • Artificial Intelligence
  • Metodologies
Integrating TDD: another step forward in code quality

At IAGT, we continue to evolve not only to optimize our work processes, but also to improve the results we deliver. We recently conducted internal training on the implementation of Test Driven Development (TDD), a technical practice that complements and enhances our agile methodologies in the team’s daily work. In this article, we will explore how TDD integrates into our workflow and the benefits it brings to our projects.

What is TDD?

Test-Driven Development (TDD) is a technique within software development that involves writing automated tests before developing the functional code. This approach is based on an iterative cycle known as “red-green-refactor”:

  • Red: Write a test that fails, defining the desired behavior.
  • Green: Write the minimum code necessary for the test to pass.
  • Refactor: Improve the code while keeping the tests green.

This process ensures that each new feature is backed by testing from conception, promoting a more thoughtful design that is oriented toward customer requirements.

The key to this step-by-step process is: “Focus on passing the test, don’t think big, think small first.”

Test-driven development (TDD) teaches us to think iteratively, promoting an incremental and reflective approach to software construction. Instead of seeking complete solutions from the outset, TDD invites us to move forward step by step, validating each small advance through automated testing.

A little history

Test-driven development (TDD) was formalized and popularized by American software engineer Kent Beck in the late 1990s as part of the Extreme Programming (XP) methodology. Although the idea of writing tests before functional code already existed in previous practices, Beck systematized it and actively promoted it. In his book Test-Driven Development: By Example (2002), Beck describes how TDD encourages simple and reliable designs by focusing development on automated tests that guide code implementation. Source.

Beck considers his contribution to be a rediscovery of the technique, as similar practices had been used previously in software development. However, his structured approach and its integration with agile methodologies such as XP were fundamental to the widespread adoption of TDD in the software industry.

Since then, TDD has become a key practice in agile development, promoting the writing of more “maintainable” code and facilitating early error detection.

TDD and agile methodologies at IAGT

At IAGT, we already use agile methodologies to manage our projects. The integration of TDD reinforces these approaches by providing a solid foundation for code quality. By writing tests before code, we encourage a more thoughtful design that is oriented towards customer requirements.

In addition, TDD facilitates continuous integration and continuous delivery (CI/CD), allowing errors to be detected early on and reducing development time.

Three keys to implementing good TDD practices

  1. Mock it. The use of mocks is essential for isolating the unit of code being tested from its external dependencies, such as databases or web services. This allows you to focus on the specific behavior of the unit under test, ensuring that tests are fast and deterministic. However, it is important to use mocks with caution, as overuse can lead to fragile tests that are overly dependent on the internal implementation.
  2. Triangulation. Triangulation is a technique that involves writing multiple tests with different inputs to guide the development of a generalized implementation. Starting with a simple implementation that satisfies the first test, additional tests are added that force the logic to be refined until a complete and robust solution is achieved. This practice helps avoid premature assumptions and promotes a more robust design.
  3. If it’s trivial, do it directly. When the solution to a test is obvious and straightforward, it is acceptable to implement it immediately without intermediate steps. This applies to cases where the logic is so simple that it does not require incremental evolution. However, it is essential to ensure that even these trivial implementations are backed by adequate testing to maintain the integrity of the TDD process.

Observed benefits

With the implementation of TDD practices at IAGT, we are seeking significant improvements in several areas:

  • Code quality: The code is cleaner, more modular, scalable, and easier to maintain.
  • Early error detection: Errors are identified and corrected in the early stages of development.
  • Confidence in development: Developers have greater confidence when making changes, knowing that tests support functionality.
  • Continuous improvement: The TDD cycle fosters a culture of continuous improvement and constant learning.

Some risks

  • Learning curve: don’t let your training and experience lead you to think that “everything is trivial”; keep thinking iteratively.
  • It does not replace other tests: performance, integration, unit tests, etc. We are not trying to cover all the code; we are trying to define a business interface.
  • A new technique: it is difficult to apply if not implemented from the outset. When I have to develop new code for a new feature, I can start to integrate this technique.

Tip: don’t go overboard or underdo it. Make a list and stick to it, as if it were a shopping list 😉

Some myths

  • TDD is just writing tests… No, it’s changing the way you program (first think about writing the test and then generate code from the test).
  • With TDD, you write twice as much code: Yes, you write more code, but it’s quality code. In the long run, it saves time because it eliminates bugs, unnecessary code, and double work.
  • TDD is only for QA: no, it’s a technique for developers.
  • It is useless in large or fast projects: no, the larger and faster it is, the more capacity for evolution it has, or the more agile it is, the more necessary it is to write a test that ensures that the changes being made comply with the standards you have written. The larger a project is, the more useful it is to have a communication interface with the business (what the application has to do) that will help us ensure that if we change something, it does not generate a b
  • It’s a technique for testing code… No, it’s a technique for changing the way you do it: I think and then I write the code.

TDD and generative AI: how to write the test so that AI develops the code for you?

The integration of generative artificial intelligence into software development has reignited interest in test-driven development (TDD). Although AIs such as ChatGPT or GitHub Copilot can generate code quickly, the question arises: how do we ensure that this code meets requirements and maintains high quality? This is where TDD provides an essential framework.

By writing the tests that define the expected behavior first, we establish clear criteria that any generated code must meet. This not only allows us to validate the functionality of the code produced by the AI, but also makes it easier to understand and maintain. Regardless of who writes the code—whether it’s a human developer or an AI—if the tests are well defined, we can trust that the code meets the established objectives.

In addition, TDD encourages more structured, requirements-driven thinking, which is especially valuable when working with AI tools. By focusing on desired outcomes before implementation, we can guide AI to generate solutions that are more aligned with our needs. This approach also facilitates refactoring and continuous code improvement, as tests act as a safety net that ensures system stability during changes.

In summary, TDD not only improves the quality of AI-generated code, but also strengthens our ability to define, validate, and maintain effective and reliable software solutions.

“We are good developers when we deeply understand what our client needs and why they need it. When faced with any technological change, it is essential to remember that understanding the purpose and requirements of the software is more important than simply writing code. The quality of our work lies in our ability to align technology with the true needs of the user.”

Eduardo Pavón, technical director of IAGT.

Training in IAGT

The adoption of agile methodologies and best practices such as TDD is an ongoing process where training, learning, and implementation are key. At IAGT, we continue to train our team by sharing best practices and refining our tools together to maximize the benefits of this practice.

Our goal? To offer high-quality software solutions that meet and exceed our customers’ expectations. But also to make our team’s day-to-day work easier and more satisfying.

What do you think? Would you like to learn more about how we work?

Write to us and let’s chat 😉

An article produced with the indispensable collaboration of our technical director at IAGT, Eduardo Pavón.

This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.