Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added article Generate Unit Tests with Copilot #44228

Merged
merged 22 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bc63871
Added article Generate Unit Tests with Copilot
sigmade Jan 12, 2025
b1c3445
Update toc.yml: add new entry for "Generate Unit Tests with GitHub Co…
sigmade Jan 18, 2025
45cecff
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
cca255e
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
3d31a3d
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
323ed56
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
c3c9ee0
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
02b7b41
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
abf7c55
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
6d128c0
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
2550a69
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
8d261e9
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
f7cb0e6
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
066b3c2
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
a975a44
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
e8c3d5e
Update docs/core/testing/unit-testing-with-copilot.md
sigmade Jan 18, 2025
327dcac
Merge branch 'main' into sigmade/tests-with-copilot
sigmade Mar 11, 2025
fbf4276
Apply suggestions from code review
IEvangelist Mar 20, 2025
6ce1c49
Apply suggestions from code review
IEvangelist Mar 20, 2025
c09d3a4
Apply suggestions from code review
IEvangelist Mar 20, 2025
6332323
Apply suggestions from code review
IEvangelist Mar 20, 2025
b242c66
Apply suggestions from code review
IEvangelist Mar 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/core/testing/media/create-unit-test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/core/testing/media/test-mehod-stub.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions docs/core/testing/unit-testing-with-copilot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
title: Generate Unit Tests with Copilot
author: sigmade
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
ms.date: 01/12/2025
---
# Generate Unit Tests with GitHub Copilot and Visual Studio

By [Yegor Sychev](https://github.com/sigmade)

This article explores how to generate unit tests and test projects in C# using the xUnit framework with the help of Visual Studio commands and GitHub Copilot.

## Generating a test project and a stub Method

There is a `ProductService` class with a `GetProductById` method that depends on the `IProductDataStorage` and `ICacheClient` interfaces.

```csharp
public class ProductService(
IProductDataStorage productDataStorage,
ICacheClient cacheClient)
{
private readonly IProductDataStorage _productDataStorage = productDataStorage;
private readonly ICacheClient _cacheClient = cacheClient;

public async Task<Product?> GetProductById(int productId)
{
var product = await _cacheClient.GetProduct(productId);

if (product != null)
{
return product;
}

product = await _productDataStorage.GetProduct(productId);

if (product != null)
{
await _cacheClient.SetProduct(product);
}

return product;
}
}
```

To generate a test project and a stub method, follow these steps:

* Select the method.
* Right-click and select 𝗖𝗿𝗲𝗮𝘁𝗲 𝗨𝗻𝗶𝘁 𝗧𝗲𝘀𝘁𝘀.

:::image type="content" source="media/create-unit-test.png" lightbox="media/create-unit-test.png" alt-text="Command 𝗖𝗿𝗲𝗮𝘁𝗲 𝗨𝗻𝗶𝘁 𝗧𝗲𝘀𝘁𝘀":::

In the dialog that appears, select xUnit from the Test Framework dropdown menu

> [!NOTE]
> 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.

:::image type="content" source="media/create-unit-test-window.png" lightbox="media/create-unit-test-window.png" alt-text="Create Unit Tests window":::

* If you don't have a test project yet, choose "New Test Project" or select an existing one.
* If necessary, specify a template for the namespace, class, and method name, then click OK.

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.

:::image type="content" source="media/test-mehod-stub.png" lightbox="media/test-mehod-stub.png" alt-text="Generated stub method":::

## Generate the tests themselves:

* Select the method being tested again.
* Right-click - Ask Copilot.
* Enter a simple prompt, such as: <br> `generate unit tests using xunit, nsubstitute and insert the result into ProductServiceTests`</br> You need to select your test class using #.

> [!TIP]
> For quick search, it is desirable that `ProductServiceTests` is open in a separate tab.

:::image type="content" source="media/test-copilot-prompt.png" lightbox="media/test-copilot-prompt.png" alt-text="Prompt for generate tests":::

Execute the prompt, click Accept, and Copilot generates the test code. After that, it remains to install the necessary packages.

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.

:::image type="content" source="media/test-mehod-stub.png" lightbox="media/test-mehod-stub.png" alt-text="Generated tests":::

Thus, using 𝗩𝗶𝘀𝘂𝗮𝗹 𝗦𝘁𝘂𝗱𝗶𝗼 in combination with 𝗚𝗶𝘁𝗛𝘂𝗯 𝗖𝗼𝗽𝗶𝗹𝗼𝘁 significantly simplifies the process of generating and writing unit tests.
Loading