-
Notifications
You must be signed in to change notification settings - Fork 6k
Added article Generate Unit Tests with Copilot #44228
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
Merged
Merged
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 b1c3445
Update toc.yml: add new entry for "Generate Unit Tests with GitHub Co…
sigmade 45cecff
Update docs/core/testing/unit-testing-with-copilot.md
sigmade cca255e
Update docs/core/testing/unit-testing-with-copilot.md
sigmade 3d31a3d
Update docs/core/testing/unit-testing-with-copilot.md
sigmade 323ed56
Update docs/core/testing/unit-testing-with-copilot.md
sigmade c3c9ee0
Update docs/core/testing/unit-testing-with-copilot.md
sigmade 02b7b41
Update docs/core/testing/unit-testing-with-copilot.md
sigmade abf7c55
Update docs/core/testing/unit-testing-with-copilot.md
sigmade 6d128c0
Update docs/core/testing/unit-testing-with-copilot.md
sigmade 2550a69
Update docs/core/testing/unit-testing-with-copilot.md
sigmade 8d261e9
Update docs/core/testing/unit-testing-with-copilot.md
sigmade f7cb0e6
Update docs/core/testing/unit-testing-with-copilot.md
sigmade 066b3c2
Update docs/core/testing/unit-testing-with-copilot.md
sigmade a975a44
Update docs/core/testing/unit-testing-with-copilot.md
sigmade e8c3d5e
Update docs/core/testing/unit-testing-with-copilot.md
sigmade 327dcac
Merge branch 'main' into sigmade/tests-with-copilot
sigmade fbf4276
Apply suggestions from code review
IEvangelist 6ce1c49
Apply suggestions from code review
IEvangelist c09d3a4
Apply suggestions from code review
IEvangelist 6332323
Apply suggestions from code review
IEvangelist b242c66
Apply suggestions from code review
IEvangelist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Generating a test project and a stub Method | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
There is a `ProductService` class with a `GetProductById` method that depends on the `IProductDataStorage` and `ICacheClient` interfaces. | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```csharp | ||
public class ProductService( | ||
IProductDataStorage productDataStorage, | ||
ICacheClient cacheClient) | ||
{ | ||
private readonly IProductDataStorage _productDataStorage = productDataStorage; | ||
private readonly ICacheClient _cacheClient = cacheClient; | ||
|
||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public async Task<Product?> GetProductById(int productId) | ||
{ | ||
var product = await _cacheClient.GetProduct(productId); | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (product != null) | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return product; | ||
} | ||
|
||
product = await _productDataStorage.GetProduct(productId); | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (product != null) | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 𝗖𝗿𝗲𝗮𝘁𝗲 𝗨𝗻𝗶𝘁 𝗧𝗲𝘀𝘁𝘀. | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
:::image type="content" source="media/create-unit-test.png" lightbox="media/create-unit-test.png" alt-text="Command 𝗖𝗿𝗲𝗮𝘁𝗲 𝗨𝗻𝗶𝘁 𝗧𝗲𝘀𝘁𝘀"::: | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In the dialog that appears, select xUnit from the Test Framework dropdown menu | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
> [!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. | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
:::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: | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* 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 #. | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
> [!TIP] | ||
> For quick search, it is desirable that `ProductServiceTests` is open in a separate tab. | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
:::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. | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
:::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. | ||
sigmade marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.