Unit and Component Testing with AI
I have a simple use case for you this week about using an open source AI model you can run locally, at no cost, that will save you about 50% of the time in creating unit and component tests, read on!
How an Open Source AI Model can Deliver 50% Time Savings
Whether we’re writing our own code or even if AI is writing the code, a decent guardrail is a strong unit and component test. Once the code is done and ready and before you call for the code review do the following:
- Feed the code into an AI model and ask it to generate unit tests that will cover the entire statement logic, branch logic and create multiple variations to check for any edge case possible
- Feed the requirements or user story into the same AI model and ask it to generate functional, component level testing that can be run in isolation from the target environment. This creates another layer of testing which is superior to unit testing and complements it. At this level of testing, you are testing the logical connections between the functions within a single component. Ask the AI model to generate stubs anywhere where it needs external input such that it is always possible to run those component tests in a simulated environment and it does not require installation on a target.
- Now run both and measure the coverage (code coverage) you got from the model. Find out where the gaps are, which lines or code segments are missing coverage and add, manually, unit tests to cover those as well
- Measure the coverage again, once it hits your favorite target (I like 100% coverage) you can move on.
- Add those tests into the team’s Continuous Integration environment and those begin to run automatically with any change. Your code is now safe from most regressions
Of course, this entire sequence can be automated such that when code creation is done, unit and component test generation is automatic and all you need to do towards the code review is to complement the missing unit tests.
Bonus activity: feed the missing unit tests into the model again to train it and track the initial coverage it generates to ensure it is improving over time.
You don’t want to use a commercially available AI engine for a couple of reasons. First, the cost is out of your control and second, the actual version used is out of your control. You can’t train it except for controlling the prompt and the version that you liked might not be available when you need it. Use an open source AI model that is embedded within your environment and is part of your source code so it can be trained and has zero cost for you. You don’t need to worry about the performance aspects of this model, it doesn’t matter if generating the unit and component tests takes a few hours vs. a few minutes with the commercial models. Time is not your bottleneck here, and you can generate those over night and have them ready for the next day.
Final Thoughts
In recent experiments we’ve run with our clients, we’ve seen how this approach, which has zero cost, has helped reduce the effort it takes to generate unit and component tests by 50% (sometimes more) and more importantly, it has helped reduce defect escapes to later phases in the SDLC at rates that have very often exceeded 30%.
So, a free enhancement to your SDLC which saves you 50% of the time you invest today and reduces 30% of your escapes. Can you find a reason not to try it out?
Try it, let me know how it goes!