Tabnine Live: Implement and validate Atlassian Jira issues with one click
October 10
8 am PT
Home / Blog /
Test-driven development in the AI era
//

Test-driven development in the AI era

//
Aydrian Howard /
3 minutes /
September 25, 2024

When’s the best time to write unit tests? Do you prefer to write code first and then follow up with unit tests? Are you the type of developer who prefers to start with the unit tests and watch as they pass and turn from red to green as you code? Or do you just skip unit testing altogether? Be honest, this is a safe space. 

According to a survey of developers published in September 2020, 41% of the respondents said their organizations choose to write their tests first and have fully adopted test-driven development (TDD).

What is test-driven development?

TDD is a software development methodology that was developed by Kent Beck in the late 1990s as part of Extreme Programming. It’s an iterative development cycle that emphasizes writing test cases before a feature or function is written. The repetition of short development cycles combines building and testing to ensure code correctness and indirectly evolves the project’s design and architecture.

The Red-Green-Refactor cycle in TDD works like this:

  1. Add a test to the test suite.
  2. (Red) Run all the tests to ensure the new test fails.

  3. (Green) Write just enough code to get that single test to pass.

  4. Run all tests.
  5. (Refactor) Improve the initial code while keeping the tests green.

  6. Repeat.

While the process can sound slow, it does improve the quality of the software in the long run. It encourages writing testable, loosely coupled code that tends to be more modular, which is easier to write, debug, understand, maintain, and resume. This comes with many benefits such as easier and faster refactoring, improved team collaboration, and increased code confidence.

Accelerating TDD with AI code assistants

Today, a well-trained large language model (LLM) with proper context is great at generating code and unit tests from requirements written in natural language. Delegating these two tasks to an AI code assistant will greatly decrease the time required for each TDD cycle. AI is also great at refactoring both the tests and the core functionality as requirements evolve. Ensuring that tests pass becomes a validation step where developers check that the test suite reports all green and the feature works as expected.

Using Tabnine doesn’t eliminate the process, but it accelerates it tenfold because you can remove much of the busy work. Here are some ways Tabnine can help:

Generating unit tests from business requirements

Once you’ve selected a test framework and set it up in your project, you can ask Tabnine Chat to generate unit tests for a feature based on business requirements. You can then run the test suite and ensure all the tests fail.

Generating code from unit tests

With unit tests for the new feature in place, you can select them and ask Tabnine to create a function that passes the selected tests. Once the generated code is implemented, you can run the test suite again and ensure the tests pass.

Refactoring as requirements evolve

It’s very likely that requirements will evolve over time. When this happens, you can select the affected tests and ask Tabnine to update them based on the new requirements. You can then go and update the related code by selecting it and asking Tabnine to refactor it based on the test file by using an @mention.

Connecting directly with your product portfolio management system

Most companies maintain product business requirements using a product portfolio management (PPM) such as Atlassian Jira. Tabnine recently released two new Jira AI agents that simplify pulling business requirements into your project context. You’ll be able to reference a Jira issue using @Jira and say, “suggest tests and/or code to pass these requirements.” You no longer need to copy/paste from Jira or even have it open. Other PPM connectors will be available in future releases.

If you’re currently using TDD, try sprinkling in some AI assistance and see the lift it provides. If you’re curious about TDD, try it using the above examples. If TDD just isn’t for you, the Tabnine Testing Agent might be right for you. Not yet a Tabnine user? Get your 90-day free trial of Tabnine Pro today.