Using Diffblue and AI for Code to Write Tests in Test Driven Development

Can I Use Diffblue Cover and AI for Code to Write Tests in Test Driven Development (TDD)?”

Here at Diffblue, there is a common question we get asked when demonstrating what our technology does, in particular Diffblue Cover (our developer support product that writes unit tests from your code using artificial intelligence - ‘AI for Code’): . It’s such a valuable question, we’ve broken it down into this post to discuss the topic in more detail.

A few reasons we get asked this question:

  1. Developers are very sensitive altering and potentially disrupting their TDD process. Some developers are almost religious about it.

  2. Managers suspect that using Diffblue Cover will make their developers lazy in TDD, which will negate the benefits of doing TDD and likely the big training, cultural and process changes they’ve already undertaken to move to TDD.

  3. At Diffblue, we get interest using Diffblue Cover for both active development using a variety of methodologies as well as inactive/legacy/maintenance development. A lot of our customers choose to start with legacy applications as the return on investment (ROI) is often easy to anticipate and definitely faster - this doesn’t mean Diffblue Cover doesn’t work in active development with TDD!

A little about Test Driven Development (TDD)

In TDD [1] , the test suite is written before the code. In short development cycles, code is written to pass discrete tests. Simply put:

  • Use TDD now = you will have cost now

  • Don’t use TDD now = you will have cost in the future [2]

A little about Diffblue Cover

Diffblue Cover is a unit test generation tool. It analyzes the Java code and writes a regression suite of unit tests automatically using pioneering artificial intelligence.

Connecting Diffblue Cover and TDD

Back to the initial question asked - “Can you use Diffblue Cover in a TDD environment?”

The answer? Absolutely!

Diffblue Cover supplements the cases you write, increasing the quality and reducing development time.

When we speak with companies that are mature using TDD, we regularly hear that the promise of code that is easier to maintain and refactor is not realized. This is typically because the level of testing being done is not sufficient. Maybe the developer only thought about the sunny day scenarios when producing the test cases. Or maybe the code was hard to test, therefore the developer didn’t put the effort in. Maybe the developer simply didn’t think about what the user was going to do. Whatever the reason, typically we see that developers have not overcome the initial cost required to get the return.

Using Diffblue Cover in a TDD environment

Given the challenges of TDD, let’s see how Diffblue Cover can help. Early in the development cycle, Diffblue Cover can produce template test cases given as little as the function signature [3]. These test cases will contain everything needed to make the test compile, run and pass. If a complex mocking environment is required, it will also provide this. This allows you to focus on creating a first-class test suite through careful consideration of what is being tested, rather than spending all your time focusing on how it is being tested.

As you prepare to merge your code, Diffblue Cover can also be used as a check and balance. By running a differential analysis, you can analyze the new code changes and get only the test cases relevant to these changes. As the test cases demonstrate untested behaviors of the system, you review them. Think to yourself, is this is a genuine behavior of my code? Is it desirable or is it a bug? By doing this we are shifting left finding bugs, which of course reduces the cost of fixing. This helps Diffblue Cover and our customers that use it to capitalise on the intended benefits of TDD.

By using Diffblue Cover within a TDD environment developers are able to reduce the time spent developing test suites by using tests writes using AI for code. Developers can also increase the quality of their code by left shifting the bug finding (and fixing) through review of changed behaviors in newly written test cases.

There you have it. Yes, Diffblue Cover can be used in a TDD environment.

Any questions? Reach out or try AI for Code yourself to learn more.

[1] https://en.wikipedia.org/wiki/Test-driven_development

[2] https://medium.freecodecamp.org/practical-tdd-test-driven-development-84a32044ed0b

[3] https://www.youtube.com/watch?v=Swus7RHh-ho