Skip to content

Commit 59c7691

Browse files
sigmadeIEvangelist
andauthored
Added article Generate Unit Tests with Copilot (#44228)
* Added article Generate Unit Tests with Copilot * Update toc.yml: add new entry for "Generate Unit Tests with GitHub Copilot and Visual Studio". * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Update docs/core/testing/unit-testing-with-copilot.md Co-authored-by: David Pine <[email protected]> * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: David Pine <[email protected]>
1 parent ec685db commit 59c7691

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed

Diff for: docs/core/testing/media/create-unit-test-window.png

43.4 KB
Loading

Diff for: docs/core/testing/media/create-unit-test.png

84.4 KB
Loading

Diff for: docs/core/testing/media/test-copilot-prompt.png

123 KB
Loading

Diff for: docs/core/testing/media/test-copilot-result.png

472 KB
Loading

Diff for: docs/core/testing/media/test-mehod-stub.png

113 KB
Loading

Diff for: docs/core/testing/unit-testing-with-copilot.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.

Diff for: docs/navigate/devops-testing/toc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ items:
5353
- name: Organize a project and test with xUnit
5454
href: ../../core/tutorials/testing-with-cli.md
5555
displayName: tutorials, cli
56+
- name: Generate unit tests with GitHub Copilot
57+
href: ../../core/testing/unit-testing-with-copilot.md
5658
- name: NUnit
5759
items:
5860
- name: C# unit testing

0 commit comments

Comments
 (0)