|
| 1 | +--- |
| 2 | +title: Generate Unit Tests with Copilot |
| 3 | +author: sigmade |
| 4 | +description: How to generate unit tests and test projects in C# using the xUnit framework with the help of Visual Studio commands and GitHub Copilot |
| 5 | +ms.date: 01/12/2025 |
| 6 | +--- |
| 7 | + |
| 8 | +# Generate unit tests with GitHub Copilot |
| 9 | + |
| 10 | +In this article, you explore how to generate unit tests and test projects in C# using the xUnit framework with the help of Visual Studio commands and GitHub Copilot. |
| 11 | + |
| 12 | +## Create a test project |
| 13 | + |
| 14 | +Imagine that there's a `ProductService` class with a `GetProductById` method that depends on the `IProductDataStorage` and `ICacheClient` interfaces. |
| 15 | + |
| 16 | +```csharp |
| 17 | +public class ProductService( |
| 18 | + IProductDataStorage productDataStorage, |
| 19 | + ICacheClient cacheClient) |
| 20 | +{ |
| 21 | + public async Task<Product?> GetProductById(int productId) |
| 22 | + { |
| 23 | + var product = await cacheClient.GetProduct(productId); |
| 24 | + |
| 25 | + if (product is not null) |
| 26 | + { |
| 27 | + return product; |
| 28 | + } |
| 29 | + |
| 30 | + product = await productDataStorage.GetProduct(productId); |
| 31 | + |
| 32 | + if (product is not null) |
| 33 | + { |
| 34 | + await _cacheClient.SetProduct(product); |
| 35 | + } |
| 36 | + |
| 37 | + return product; |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +To generate a test project and a stub method, follow these steps: |
| 43 | + |
| 44 | +- Select the method. |
| 45 | +- Right-click and select **Create Unit Tests**. |
| 46 | + |
| 47 | +:::image type="content" source="media/create-unit-test.png" lightbox="media/create-unit-test.png" alt-text="Command Create Unit Tests"::: |
| 48 | + |
| 49 | +In the **Create Unit Tests** dialog, select **xUnit** from the **Test Framework** dropdown menu. |
| 50 | + |
| 51 | +> [!NOTE] |
| 52 | +> The **Create Unit Tests** command defaults to the MSTest framework. However, since this example uses xUnit, you need to install the Visual Studio extension [xUnit.net.TestGenerator2022](https://marketplace.visualstudio.com/items?itemName=YowkoTsai.xunitnettestgenerator2022). |
| 53 | +
|
| 54 | +:::image type="content" source="media/create-unit-test-window.png" lightbox="media/create-unit-test-window.png" alt-text="Create Unit Tests window"::: |
| 55 | + |
| 56 | +* If you don't have a test project yet, choose "New Test Project" or select an existing one. |
| 57 | +* If necessary, specify a template for the namespace, class, and method name, then click OK. |
| 58 | + |
| 59 | +After a few seconds, Visual Studio will pull in the necessary packages, and we will get a generated xUnit project with the required packages, structure, a reference to the project being tested, and with the `ProductServiceTests` class and a stub method. |
| 60 | + |
| 61 | +:::image type="content" source="media/test-mehod-stub.png" lightbox="media/test-mehod-stub.png" alt-text="Generated stub method"::: |
| 62 | + |
| 63 | +## Generate the tests themselves |
| 64 | + |
| 65 | +- Select the method being tested again. |
| 66 | +- Right-click - **Ask Copilot**. |
| 67 | +- Enter a simple prompt, such as: |
| 68 | + |
| 69 | + "generate unit tests using xunit, nsubstitute and insert the result into #ProductServiceTests file." |
| 70 | + |
| 71 | + You need to select your test class when you type the `#` character. |
| 72 | + |
| 73 | +> [!TIP] |
| 74 | +> For quick search, it's desirable that `ProductServiceTests` is open in a separate tab. |
| 75 | +
|
| 76 | +:::image type="content" source="media/test-copilot-prompt.png" lightbox="media/test-copilot-prompt.png" alt-text="Prompt for generate tests"::: |
| 77 | + |
| 78 | +Execute the prompt, click **Accept**, and Copilot generates the test code. After that, it remains to install the necessary packages. |
| 79 | + |
| 80 | +When the packages are installed, the tests can be run. This example worked on the first try: Copilot knows very well how to work with NSubstitute, and all dependencies were defined through interfaces. |
| 81 | + |
| 82 | +:::image type="content" source="media/test-mehod-stub.png" lightbox="media/test-mehod-stub.png" alt-text="Generated tests"::: |
| 83 | + |
| 84 | +Thus, using **Visual Studio** in combination with **GitHub Copilot** significantly simplifies the process of generating and writing unit tests. |
0 commit comments