News: 1600683312

  ARM Give a man a fire and he's warm for a day, but set fire to him and he's warm for the rest of his life (Terry Pratchett, Jingo)

Coding unit tests is boring. Wouldn't it be cool if an AI could do it for you? That's where Diffblue comes in

(2020/09/21)


Oxford-based Diffblue has claimed its AI will automate one of the most important but tedious tasks in software development: writing unit tests.

Test-driven development (TDD) is a methodology invented – or, as he has said, rediscovered – by Kent Beck, who wrote a unit test framework for Smalltalk in the late '80s. The idea of exercising code with unit tests, which run the code and check that the output is as expected, is now widely accepted as best practice.

Unit tests help to avoid regressions – bugs introduced into code that previously worked correctly – and are a critical part of CI/CD (Continuous Integration and Continuous Delivery) since they give the developer confidence that an application or service still works after they add or modify the code. It is therefore hard to maintain rapid velocity – frequent releases – without rigorous unit testing. The popular SQLite database engine has [1]640 times as much testing code as code in the engine itself.

Writing unit tests may be important, but it is less interesting than adding features. "It is tedious grunt work; it's very important, but it is the first thing to go when the team is under time pressure," Matthew Lodge, CEO of Diffblue, told The Register . "It's something that humans are not very good at, and they make lots of mistakes as well because it's boring."

Take Cover...

Diffblue was spun out of the University of Oxford following research into how to use AI to write tests automatically. There are already plenty of tools that generate unit tests, but in general they are template-based and rely on developers to add the logic. Diffblue's [2]Cover , on the other hand, writes everything. "We write a full set of unit tests that compile and pass. It's a full unit test suite that reflects the current behaviour of the program so that when you make a change, you can find out from the test behaviour what you have changed and so you catch regressions," said Lodge.

[3]

Diffblue Cover running AI-generated tests on the sample Spring Boot application Petclinic (click to enlarge)

Cover has now been released as a free Community Edition. It only works with Java, and the only IDE integration is with IntelliJ IDEA, though the paid-for version also has a command-line option.

"As a small company we want to do one thing really well first," said Lodge. "The core technology is language independent so when we analyse the program we build a model of the program that we can reason about, then we are running tests, we again use a generic representation of the test which we then translate into Java."

Lodge said that JavaScript and Python are common requests, as is support for Visual Studio Code for which there is already an early alpha version.

Let's have a play then

We wrote a new method for the Spring Boot Petclinic sample, which includes a database of pets and their owners. Our method is HasPet() , which determines whether an owner actually has a pet. Right-click the method, select Write Test, and Cover generates two test methods. The first creates a new owner but no pet, calls the method and asserts that it is false. The second test creates a new owner and a pet, assigns the pet to the owner, calls the method and asserts it to be true. Impressive.

There is a snag, though. We modified HasPet() so it has a bug. It now returned true when it should be false, and vice versa. We asked Cover to generate new tests. The new tests passed since Cover did not know the intent of the code, only what it actually did. That said, Cover left the old tests in place, and they duly failed, so we did have some clue that there was a problem. Had we written the bug in the original code, though, the Cover test would have been useless – unless, perhaps, the developer inspected the test code and questioned its assertions.

Lodge acknowledged the problem, telling us: "The code might have bugs in it to begin with, and we can't tell if the current logic that you have in the code is correct or not, because we don't know what the intent is of the programmer, and there's no good way today of being able to express intent in a way that a machine could understand.

"That is generally not the problem that most of our customers have. Most of our customers have very few unit tests, and what they typically do is have a set of tests that run functional end-to-end tests that run at the end of the process."

Lodge's argument is that if you start with a working application, then let Cover write tests, you have a code base that becomes amenable to high velocity delivery. "Our customers don't have any unit tests at all, or they have maybe 5 to 10 per cent coverage. Their issue is not that they can't test their software: they can. They can run end-to-end tests that run right before they cut a release. What they don't have are unit tests that enable them to run a CI/CD pipeline and be able to ship software every day, so typically our customers are people who can ship software twice a year."

[4]

Diffblue Cover creating tests for the Petclinic application (click to enlarge)

The reason for the lack of unit tests may be time pressure or may be historical. "Most organisations build on existing applications, and that is the biggest challenge for folks like banks. You have all of this Java code that basically runs the bank, you have a way to ship it, because you have tests that you can run at the end of the process, but what you don't have are tests that you can run after every single commit."

How does Diffblue Cover work? "It's a combination of static and dynamic analysis," said Lodge. "We write what we think is a good test to get a starter. Then we run it against the code and we observe the behaviour of the method. From running it we can see what the method does, with side effects as well as the return value, and then we go looking for a better test than the one that we generated. Then it's a probabilistic search of the space of possible test cases."

Interested parties can review some of the research behind this process on the [5]Diffblue site .

Diffblue emerged out of a partnership with Goldman Sachs, hence its skew towards the banking sector. "Goldman Sachs followed the company because they were very interested in the technology, Goldmans helped us build the product and essentially we built the first version with Goldman's help," said Lodge. "What you see today in the community edition is version 2 of the product, with everything we learned from that first experience. There hasn't been a tool like this before. The purpose of the Community Edition is to have a free way for people to see what the tool can do.

"We can write a test with full mocking in about 600 milliseconds. So we are 10 to 100 times faster than humans at writing these tests."

Cover does a great job of exercising the developer's code, but unfortunately only a human will know if it is working as intended. ®

Get our [6]Tech Resources



[1] https://sqlite.org/testing.html

[2] https://www.diffblue.com/products/

[3] https://regmedia.co.uk/2020/09/18/diffbluemain.jpg

[4] https://regmedia.co.uk/2020/09/18/createtests.png

[5] https://www.diffblue.com/research

[6] https://whitepapers.theregister.com/

Automatic test case generation

Anonymous Coward

Whilst this can be a major benefit, never forget that there still needs to be a set of tests against requirements.

"Does what it says in the code"

Gonzo wizard

This kind of mass, automatic test case generation is only useful in documenting the current behaviour of the code - bugs and all. It's the kind of thing I'd use once when taking over a codebase with poor coverage at the unit level, and then never again. But that's because maintaining good quality tests that cover what should happen, with a high coverage percentage, is inherently a human activity driven by knowledge of requirements.

Functional

Julz

Tests have their place (just mocking api's doesn't seem very adequate...) but the devil is always in the non-functional testing space. If this was truly AI, then it could be of use in this space. As it's not, it can't.

Edit: Oh, and writing adequate unit tests is part of the job and if you deliver code into production without them you should be shown the comfy chair.

Unit tests in an ideal world

spireite

..... i'd agree, but they are ALWAYS the first thing to go. In fact, I've found it more prevalent in Agile processes to not actually get done at all

That reason seems to be that since Agile is abused so much as an excuse to make changes/change minds on functionality that apathy sets in very quickly... and once it does, it is tremendously difficult to back out of that.

It doesn't help when companies still (and lots of them do) have the viewpoint that time spent on TDD is wasted upfront time

One and the same...

RyokuMas

"Writing unit tests may be important, but it is less interesting than adding features."

... but needlessly fixing regression bugs in code that has grown organically over time to meet client needs is even less interesting/more soul destroying still. Especially when you've got an incomplete test suite (or none at all) to back you up.

As developers, we need to embrace and champion the idea that unit tests are part of anything we develop, be it shiny new feature or extension as demanded by the client (especially so in this last case when the account managers start trying to push deadlines on us!)

I recall a good while back reading this utterly moronic statement that developers should be able to write their code in such a way that it anticipates all future needs and thus will never need testing again - requirements are fluid, therefore code has to be as well. It's up to us to ensure that these changes we make are an evolution rather than a cancerous growth, and unit tests are a key component of this.

Nice

parperback parper

So, it writes characterisation tests, not unit tests.

That I can get behind (although not use until it comes to C++)

sanmigueelbeer

Boring.

Wake me up when AI has come up with a methodology to do away AGILE (and their practitioner).

"I think trash is the most important manifestation of culture we have in my
lifetime."
-- Johnny Legend