Published on: December 2, 2024
4 min read
Learn how we used the AI-driven DevSecOps platform to generate automated tests and improve our development speed and quality.
Automated testing is time-consuming and can feel like it’s not moving a project forward. However, as many developers have likely experienced, automated testing provides an overall positive return on investment. In building a custom module (we'll call it gitlab-helper for this article), this was particularly true.
Our initial development focused on migrating tried and used functionality from existing scripts to a new module whose sole purpose was to serve as a baseline for future functionality. Although existing scripts lacked automated testing, their consistent usage was strong anecdotal evidence the functionality worked as expected.
Our objective was to deliver a more mature solution to this problem, so automated testing became a necessity. This introduced the challenge of building efficiently, while balancing the time to test and ensure a robust product; and with a total of three team members, this was no small bottleneck. Therefore, the team decided to take advantage of GitLab Duo, our suite of AI capabilities, for test generation, improving speed and quality of the delivered product.
In this three-part series on automating with GitLab Duo, we will cover:
While functionality is available across tools, this article will cover using GitLab Duo in VS Code, with the GitLab Workflow extension for VS Code to generate tests. Links to other GitLab Duo options are available in the references below.
As a prerequisite to using GitLab Duo, we ensured we had a GitLab Duo-enabled account. If you don't have GitLab Duo, you can sign up for a free 60-day trial.
To use GitLab Duo Chat in VS Code, we followed the instructions for installation. Then, we were able to see the GitLab Duo Chat extension on the sidebar and open the Chat window.
gitlab-helper is a custom module built for standardizing interaction with the GitLab API across the team's work and extends other library functionalities to simplify development and scripting work. Once a method or feature was migrated to gitlab-helper and appeared to be implemented appropriately, the process to generate tests for it was simple:
Within a few seconds, tests were generated and presented in the GitLab Duo Chat window. These tests can be reviewed and or added to the codebase, via copy/paste, into existing or new test files. As is the case with most natural language processing generations today, particularly around context, some of the initial tests created by GitLab Duo failed, thus requiring finetuning (for instance, when dealing with nested dependencies).
Pro tip: GitLab Duo does not auto-create files to add generated tests to. We found it was helpful to create new test files and add a
# Tests Generated by Duo
comment at the top of them and suffix them with_duo.py
to indicate where the tests came from.
GitLab Duo provided a great starting point for building out gitlab-helper’s automated testing and greatly improved test writing efficiency and code coverage, speeding up the development process substantially. Alongside GitLab Duo, numerous iterations of valuable tests were introduced into the gitlab-helper module with human oversight.
Read the next installment in this series where we share what we learned while using GitLab Duo for generating automated tests and working interactively with AI for more complex situations.
There’s more than one way to use GitLab Duo to generate tests, check out the other options below: