Skip to content

Commit f4bd584

Browse files
authored
Add missing step (dotnet#25437)
1 parent ef12d45 commit f4bd584

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

docs/core/testing/unit-testing-with-dotnet-test.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Unit testing C# code in .NET Core using dotnet test and xUnit
33
description: Learn unit test concepts in C# and .NET Core through an interactive experience building a sample solution step-by-step using dotnet test and xUnit.
44
author: ardalis
55
ms.author: wiwagn
6-
ms.date: 10/21/2020
6+
ms.date: 08/02/2021
77
---
88
# Unit testing C# in .NET Core using dotnet test and xUnit
99

@@ -81,9 +81,10 @@ The following instructions provide the steps to create the test solution. See [C
8181
* The preceding command:
8282
* Creates the *PrimeService.Tests* project in the *PrimeService.Tests* directory. The test project uses [xUnit](https://xunit.net/) as the test library.
8383
* Configures the test runner by adding the following `<PackageReference />`elements to the project file:
84-
* "Microsoft.NET.Test.Sdk"
85-
* "xunit"
86-
* "xunit.runner.visualstudio"
84+
* `Microsoft.NET.Test.Sdk`
85+
* `xunit`
86+
* `xunit.runner.visualstudio`
87+
* `coverlet.collector`
8788

8889
* Add the test project to the solution file by running the following command:
8990

@@ -167,7 +168,8 @@ Run `dotnet test`. The test passes.
167168

168169
### Add more tests
169170

170-
Add prime number tests for 0 and -1. You could copy the preceding test and change the following code to use 0 and -1:
171+
Add prime number tests for 0 and -1. You could copy the test created in the preceding step and make copies of the following code to test 0 and -1.
172+
But don't do it, as there's a better way.
171173

172174
```csharp
173175
var primeService = new PrimeService();
@@ -196,11 +198,15 @@ public void IsPrime_InputIs1_ReturnFalse()
196198

197199
with the following code:
198200

199-
[!code-csharp[Sample_TestCode](../../../samples/snippets/core/testing/unit-testing-using-dotnet-test/csharp/PrimeService.Tests/PrimeService_IsPrimeShould.cs?name=Sample_TestCode)]
201+
:::code language="csharp" source="../../../samples/snippets/core/testing/unit-testing-using-dotnet-test/csharp/PrimeService.Tests/PrimeService_IsPrimeShould.cs" id="Sample_TestCode":::
200202

201203
In the preceding code, `[Theory]` and `[InlineData]` enable testing several values less than two. Two is the smallest prime number.
202204

203-
Run `dotnet test`, two of the tests fail. To make all of the tests pass, update the `IsPrime` method with the following code:
205+
Add the following code after the class declaration and before the `[Theory]` attribute:
206+
207+
:::code language="csharp" source="../../../samples/snippets/core/testing/unit-testing-using-dotnet-test/csharp/PrimeService.Tests/PrimeService_IsPrimeShould.cs" id="Sample_InitCode":::
208+
209+
Run `dotnet test`, and two of the tests fail. To make all of the tests pass, update the `IsPrime` method with the following code:
204210

205211
```csharp
206212
public bool IsPrime(int candidate)

samples/snippets/core/testing/unit-testing-using-dotnet-test/csharp/PrimeService.Tests/PrimeService_IsPrimeShould.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ namespace Prime.UnitTests.Services
66
{
77
public class PrimeService_IsPrimeShould
88
{
9+
#region Sample_InitCode
910
private readonly PrimeService _primeService;
1011

1112
public PrimeService_IsPrimeShould()
1213
{
1314
_primeService = new PrimeService();
1415
}
16+
#endregion
1517

1618
#region Sample_TestCode
1719
[Theory]

0 commit comments

Comments
 (0)