In recent months, the buzz around AI assistants, particularly our "old new" friend ChatGPT, has been impossible to ignore. If you've ever requested it to generate code for you, chances are you received a fairly decent result. However, it likely required some adjustments to align with your personal (or corporate) style and conventions. In this article, I'll reveal a simple yet effective trick to make ChatGPT generate unit tests precisely the way you want.
While I'll be using .NET in the examples below, this method applies to any programming language. So, ensure you have access to ChatGPT-4 (a statement that will look fun a few years from now when we're all using ChatGPT-10) and let's get started.
As I was writing this article, OpenAI temporarily (?) disabled the "Browse with Bing" feature. This led me to put it on hold until I recently discovered the Link Reader plugin, which performs even better than the slow and glitchy "Browse with Bing" (despite its complaints about fetching failures, it always got the job done).
💡Approach #1
The first step is to create an example for ChatGPT to follow, ensuring the generated code meets our standards. Alongside the example, we need to clearly explain our requirements.
I've created a GitHub Gist containing my preferred unit test template, which I discussed in the"Unit Tests Done Right" article, and provided some instructions for the language model.
🚀 Prompt
I want you to write unit tests for my c# class. Use best coding practices, AAA and the following libraries: xUnit, FluentAssertions, AutoFixture, NSubstitute. I'm going to give you a link to a GitHub gist which contains an example of the code style, structure and conventions that you will have to follow.
This is the example: https://gist.github.com/Powerz/f20500a16eead6bc591ed99d224700f5
And this is the class you need to write unit tests for:
(your code)
👍 Result
Our robot friend generated an impressive output that closely matched the desired code style and required minimal changes. The only aspect that caught my attention was the use of hardcoded input values, while I prefer to generate test data using either AutoFixture or [AutoData]
. However, this was the case only with "Browse with Bing". With the "Link Reader", the code was generated exactly as shown in the GitHub Gist.
💡Approach #2
Next, I wondered if I could simply direct ChatGPT to my blog posts where I've detailed my approach to unit tests. Instead of providing instructions and a template link, I could ask it to read the articles and follow the practices described therein.
🚀 Prompt
I want you to write unit tests for my c# class. I'm going to give you a link to my blog posts which contain examples of the code style, structure and conventions that you will have to follow.
Here are the links: https://zagosk.in/unit-tests-done-right, https://zagosk.in/unit-tests-done-right-part-2
And this is the class you need to write unit tests for:
(your code)
🔥 Result
This time the output was perfect in both cases. Although when using "Browse with Bing" the machine complained that it had some problems reading the second article, it managed to read the first one and it just nailed it. The code was perfect and didn't require any changes. With the Link Reader plugin it was much faster and error-less.
This time, the output was flawless in both instances. Although "Browse with Bing" reported some difficulties reading the second article, it successfully read the first one and the resulting code was impeccable, requiring no changes. The Link Reader plugin proved to be much faster and error-free.
Is ChatGPT-3.5 Capable?
With ChatGPT-3.5 it's not that simple and it's not due to the lack of Web Search, but simply because v3.5 is not as progressive as v4. Although I couldn't make this model write tests the way I wanted, if v3.5 is the only model you have access to, give it a go and maybe the result will be acceptable.
With ChatGPT-3.5, things aren't as straightforward, and it's not due to the absence of Web Search: v3.5 just isn't as advanced as v4. While I couldn't get this model to write tests to my specifications, if v3.5 is the only model you have access to, it's worth a shot. Play with it, and you might find the results acceptable.
In Conclusion
Thank you for your time. If you're reading this, there's a good chance you found it useful, which makes me happy Harness the power of cloud brains to automate boring routine tasks, validate your ideas, and devote more time to challenges that only your brain can handle (at least for now).
That's probably enough about ChatGPT for this blog. Next time, I plan to shift gears and discuss something less hyped.
Cheers!
Top comments (0)