Creating Tests with GitHub Copilot for Visual Studio
Published Apr 11 2024 12:11 AM 1,855 Views
Microsoft

One of the recurring jokes in our industry is that developers are not very good at two things when coding: Documenting code, and creating unit tests. These are two areas where GitHub Copilot can help! Let's see how in the new short video that I just published.

 

 

The slash commands, "magic spells" for Visual Studio

In previous videos, we talked about the availability of "Slash commands" in Visual Studio. These can be invoked by typing a Slash character '/' in a GitHub Copilot chat dialog. This will open a context menu where you can select a command, for example /fix to fix some code, or /optimize like we saw in our previous article. We also have /doc which, as the name shows, will create documentation for the code currently in context, for example a method or a property.

 

LBugnion_1-1712817804983.png

 

The last command available on the screenshot above is /tests. That one is particularly useful to help you getting started with unit tests for the current code in context. In the new short video I just published, Bruno Capuano shows how GitHub Copilot can propose unit tests for a whole class. 

In order to do that, Bruno starts by typing /tests in the Copilot chat window, and then type a hash '#' which will open another context menu to select the context.

 

The importance of context

For Large Language Models (LLMs), context is crucial to the good execution of the prompt. The context will be injected in the prompt together with the user input. We talk about metaprompts. In other words, everything that can be used to help the LLM to generate more precise and factual outputs is useful. In GitHub Copilot for Visual Studio, the context can be the currently selected code, another code file, or even the whole Solution. These options can be selected from the "hash context menu" as shown here:

 

LBugnion_3-1712818159137.png

 

Creating the tests

Once this command is executed, Copilot chat will propose an outline of what the tests could look like. Note that like with every other feature of GitHub Copilot, this is meant to assist you, the Developer-in-Charge, and not to just do the work for you. The code created must be verified and approved by you, and thoroughly tested to make sure that it actually does what you expected (yes, you are going to test your unit tests :smile:). Copilot is making you faster and more efficient, but it is not replacing your role.

The particularity here, compared to previous features we showed, is that Copilot proposes to create the tests in a new file. This is usually what we do for unit tests, with dedicated classes and methods. This is available here with the button labeled Insert in new file. Once the new file is created, you must save it to the solution. In fact, you will probably want to arrange your tests in a separate class library in the same solution.

 

More information

We have compiled a list of resources here, and we hope that this information is useful to get you started and improve your best practices in Visual Studio. You can also see the related 13 minutes long video here, and see all the videos in this series here. Stay tuned to more content by subscribing to this blog, and to the Visual Studio YouTube channel.

 

Co-Authors
Version history
Last update:
‎Apr 11 2024 12:12 AM
Updated by: