Make Your Dev Life Easier by Generating Tests with CodiumAI
Many developers still claim they don’t like writing tests, so the idea of generating them using AI was always going to appeal. Meet CodiumAI.
Nov 18th, 2023 4:00am by
Image via CodiumAI website
- We cannot withdraw more money than we have.
- If the input deposit or withdrawal value is zero, we should not proceed.
- Neither the requested deposit or withdrawal amounts should be negative.
After some authentication tango, I am in. At some point .NET was loaded in and I got it to create a simple console project. Here is the code:
public class BankAccount
{
private uint balance;
public string ShowBalance() => $"{CURRENCYSIGN}{balance}";
private const string CURRENCYSIGN = "$";
public void Deposit(uint funds)
{
balance = balance + funds;
Console.WriteLine($"{CURRENCYSIGN}{funds} deposited. Balance now {ShowBalance()}");
}
public void Withdraw(uint funds)
{
if (funds > balance) throw new Exception($"Cannot withdraw {CURRENCYSIGN}{funds}, balance is {ShowBalance()}");
balance = balance - funds;
Console.WriteLine($"{CURRENCYSIGN}{funds} withdrawn. Balance now {ShowBalance()}");
}
static void Main(string[] args)
{
BankAccount ba = new BankAccount();
try {
// Money in
ba.Deposit(20);
//Money out
ba.Withdraw(5);
//Too much ba.Withdraw(25);
}
catch (System.Exception ex)
{
Console.WriteLine($"Error {ex.Messag}");
}
}
}
BankAccount> dotnet run
$20 deposited. Balance now $20
$5 withdrawn. Balance now $15
Error Cannot withdraw $25, balance is $15
First of all, the AI-generated a very acceptable English summary of the class:
It gives an English explanation for the three methods. It also generated “code suggestions”, and these were all sensible to some extent. For example, it suggested using decimal values instead of an unsigned int. I will leave the reader, if they are interested, to consider the pros and cons of that.
This gives me confidence that the tests it generates will be good. And they are.
The presentation is very nice. It generates some tests and summarizes other tests it could generate. First, the Happy Path:
Here are two of the generated Happy Path examples:
[Test]
public void test_deposit()
{
BankAccount ba = new BankAccount();
ba.Deposit(20); Assert.AreEqual("$20", ba.ShowBalance());
}
[Test]
public void test_withdraw_more_than_balance()
{
BankAccount ba = new BankAccount();
Assert.Throws<Exception>(() => ba.Withdraw(25));
}
OK, let’s look at the edge cases:
These deal with the zero unit withdrawals — this implies that I can extend these tests and my code to satisfy my need to check that callouts to real systems are not done if not required. Note that you can generate and regenerate a test as needed.
Finally the “other” cases:
The one of interest here is the one that tests the maximum value — if we were not sure about using an unsigned integer, we would want to check on any regression cases concerning changes in the size of the account.
Conclusion
Looking at this tool in the context of available AI tools, I think it is a good example of AI helping with the grunt work of developer testing, while not doing anything too unexpected. When you build up a test framework, you will need set up and tear down methods, and supporting functions that might mean you can’t just drop generated tests directly into your code without alteration. But from my own testing, I would recommend trying CodiumAI out to see if it can be part of your work day. Well, give it a test run anyway.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.