You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `IsTestProject` property signifies that a project is a test project. When this property is set to `true`, validation to check if the project references a self-contained executable is disabled. That's because test projects have an `OutputType` of `Exe` but usually call APIs in a referenced executable rather than trying to run. In addition, if a project references a project where `IsTestProject` is set to `true`, the test project isn't validated as an executable reference.
1489
-
1490
-
This property is mainly needed for the `dotnet test` scenario and has no impact when using *vstest.console.exe*.
1491
-
1492
-
> [!NOTE]
1493
-
> If your project specifies the [MSTest SDK](../testing/unit-testing-mstest-sdk.md), you don't need to set this property. It's set automatically. Similarly, this property is set automatically for projects that reference the Microsoft.NET.Test.Sdk NuGet package linked to VSTest.
@@ -1535,6 +1525,10 @@ The `EnableMSTestRunner` property enables or disables the use of the [MSTest run
1535
1525
1536
1526
The `EnableNUnitRunner` property enables or disables the use of the [NUnit runner](../testing/unit-testing-nunit-runner-intro.md). The NUnit runner is a lightweight and portable alternative to VSTest. This property is available in [NUnit3TestAdapter](https://www.nuget.org/packages/NUnit3TestAdapter) in version 5.0 and later.
1537
1527
1528
+
## UseMicrosoftTestingPlatformRunner
1529
+
1530
+
The `UseMicrosoftTestingPlatformRunner` property enables or disables the use of Microsoft.Testing.Platform runner in [xUnit.v3](https://xunit.net) test projects.
1531
+
1538
1532
### GenerateTestingPlatformEntryPoint
1539
1533
1540
1534
Setting the `GenerateTestingPlatformEntryPoint` property to `false` disables the automatic generation of the program entry point in test projects that use [Microsoft.Testing.Platform](../testing/microsoft-testing-platform-intro.md). You might want to set this property to `false` when you manually define an entry point, or when you reference a test project from an executable that also has an entry point.
@@ -1543,6 +1537,10 @@ For more information, see [error CS8892](../testing/microsoft-testing-platform-f
1543
1537
1544
1538
To control the generation of the entry point in a VSTest project, use the `GenerateProgramFile` property.
1545
1539
1540
+
### GenerateTestingPlatformConfigurationFile
1541
+
1542
+
The `GenerateTestingPlatformConfigurationFile` property is only available when [IsTestingPlatformApplication](#istestingplatformapplication) is `true`. It's used to allow the copy and rename of the [config file](../testing/microsoft-testing-platform-config.md) in the output folder.
1543
+
1546
1544
### TestingPlatformCaptureOutput
1547
1545
1548
1546
The `TestingPlatformCaptureOutput` property controls whether all console output that a test executable writes is captured and hidden from the user when you use `dotnet test` to run `Microsoft.Testing.Platform` tests. By default, the console output is hidden. This output includes the banner, version information, and formatted test information. Set this property to `false` to show this information together with MSBuild output.
@@ -1562,9 +1560,12 @@ The `TestingPlatformCaptureOutput` property lets you specify command-line argume
1562
1560
1563
1561
### TestingPlatformDotnetTestSupport
1564
1562
1565
-
The `TestingPlatformDotnetTestSupport` property lets you specify whether VSTest is used when you use`dotnet test` to run tests. If you set this property to `true`, VSTest is disabled and all `Microsoft.Testing.Platform` tests are run directly.
1563
+
The `TestingPlatformDotnetTestSupport` property enables testing Microsoft.Testing.Platform apps when using the VSTest mode of`dotnet test`.
1566
1564
1567
-
If you have a solution that contains VSTest test projects as well as MSTest, NUnit, or XUnit projects, you should make one call per mode (that is, `dotnet test` won't run tests from both VSTest and the newer platforms in one call).
1565
+
> [!NOTE]
1566
+
> Don't call `dotnet test` on a solution that has both VSTest and Microsoft.Testing.Platform projects, as that scenario is not supported.
1567
+
1568
+
For more information, see [Testing with 'dotnet test'](../testing/unit-testing-with-dotnet-test.md).
1568
1569
1569
1570
### TestingPlatformShowTestsFailure
1570
1571
@@ -1582,10 +1583,30 @@ When you use the [MSTest project SDK](../testing/unit-testing-mstest-sdk.md), th
1582
1583
1583
1584
For more information, see [Microsoft.Testing.Platform profile](../testing/unit-testing-mstest-sdk.md#microsofttestingplatform-profile).
1584
1585
1586
+
## VSTest–related properties
1587
+
1588
+
The following MSBuild properties are documented in this section:
1589
+
1590
+
-[IsTestProject](#istestproject)
1591
+
-[UseVSTest](#usevstest)
1592
+
1593
+
### IsTestProject
1594
+
1595
+
The `IsTestProject` property is set to `true` by the [Microsoft.NET.Test.Sdk NuGet package](https://www.nuget.org/packages/Microsoft.NET.Test.Sdk). It signifies whether a project is a VSTest test project so that it's recognized by `dotnet test`.
1596
+
1597
+
> [!NOTE]
1598
+
> If your project specifies the [MSTest SDK](../testing/unit-testing-mstest-sdk.md), you don't need to set this property, as MSTest.Sdk references the Microsoft.NET.Test.Sdk NuGet package.
1599
+
1585
1600
### UseVSTest
1586
1601
1587
1602
Set the `UseVSTest` property to `true` to switch from Microsoft.Testing.Platform to the [VSTest](/visualstudio/test/vstest-console-options) runner when using the [MSTest project SDK](../testing/unit-testing-mstest-sdk.md).
1588
1603
1604
+
## MSTest–related properties
1605
+
1606
+
The following MSBuild properties are documented in this section:
1607
+
1608
+
-[MSTestAnalysisMode](#mstestanalysismode)
1609
+
1589
1610
### MSTestAnalysisMode
1590
1611
1591
1612
This property decides which analyzers are enabled at which severity. For more information, see [MSTest code analysis](../testing/mstest-analyzers/overview.md).
Copy file name to clipboardExpand all lines: docs/core/testing/unit-testing-csharp-with-xunit.md
+37-43Lines changed: 37 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ ms.date: 03/07/2024
7
7
---
8
8
# Unit testing C# in .NET using dotnet test and xUnit
9
9
10
-
This tutorial shows how to build a solution containing a unit test project and source code project. To follow the tutorial using a pre-built solution, [view or download the sample code](https://github.com/dotnet/samples/tree/main/core/getting-started/unit-testing-using-dotnet-test/). For download instructions, see [Samples and Tutorials](../../samples-and-tutorials/index.md#view-and-download-samples).
10
+
This tutorial shows how to build a solution containing a unit test project and source code project. To follow the tutorial using a prebuilt solution, [view or download the sample code](https://github.com/dotnet/samples/tree/main/core/getting-started/unit-testing-using-dotnet-test/). For download instructions, see [Samples and Tutorials](../../samples-and-tutorials/index.md#view-and-download-samples).
11
11
12
12
## Create the solution
13
13
@@ -60,11 +60,7 @@ The following instructions provide the steps to create the test solution. See [C
60
60
}
61
61
```
62
62
63
-
* The preceding code:
64
-
* Throws a <xref:System.NotImplementedException> with a message indicating it's not implemented.
65
-
* Is updated later in the tutorial.
66
-
67
-
<!-- preceding code shows an english bias. Message makes no sense outside english -->
63
+
Currently this code throws a <xref:System.NotImplementedException>, but you'll implement the method later in the tutorial.
68
64
69
65
* In the *unit-testing-using-dotnet-test* directory, run the following command to add the class library project to the solution:
70
66
@@ -78,13 +74,12 @@ The following instructions provide the steps to create the test solution. See [C
78
74
dotnet new xunit -o PrimeService.Tests
79
75
```
80
76
81
-
* The preceding command:
82
-
* Creates the *PrimeService.Tests* project in the *PrimeService.Tests* directory. The test project uses [xUnit](https://xunit.net/) as the test library.
83
-
* 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`
87
-
*`coverlet.collector`
77
+
The preceding command creates the *PrimeService.Tests* project in the *PrimeService.Tests* directory. The test project uses [xUnit](https://xunit.net/) as the test library. The command also configures the test runner by adding the following `<PackageReference />`elements to the project file:
78
+
79
+
*`Microsoft.NET.Test.Sdk`
80
+
*`xunit`
81
+
*`xunit.runner.visualstudio`
82
+
*`coverlet.collector`
88
83
89
84
* Add the test project to the solution file by running the following command:
90
85
@@ -121,33 +116,33 @@ Follow the instructions for "Replace the code in *PrimeService.cs* with the foll
121
116
122
117
## Create a test
123
118
124
-
A popular approach in test driven development (TDD) is to write a (failing) test before implementing the target code. This tutorial uses the TDD approach. The `IsPrime` method is callable, but not implemented. A test call to `IsPrime` fails. With TDD, a test is written that is known to fail. The target code is updated to make the test pass. You keep repeating this approach, writing a failing test and then updating the target code to pass.
119
+
A popular approach in test driven development (TDD) is to write a (failing) test before implementing the target code. This tutorial uses the TDD approach. The `IsPrime` method is callable but not implemented. A test call to `IsPrime` fails. With TDD, you write a test that's known to fail. Then you update the target code to make the test pass. You keep repeating this approach, writing a failing test and then updating the target code to pass.
125
120
126
121
Update the *PrimeService.Tests* project:
127
122
128
123
* Delete *PrimeService.Tests/UnitTest1.cs*.
129
124
* Create a *PrimeService.Tests/PrimeService_IsPrimeShould.cs* file.
130
125
* Replace the code in *PrimeService_IsPrimeShould.cs* with the following code:
131
126
132
-
```csharp
133
-
usingXunit;
134
-
usingPrime.Services;
127
+
```csharp
128
+
usingXunit;
129
+
usingPrime.Services;
135
130
136
-
namespacePrime.UnitTests.Services
137
-
{
138
-
publicclassPrimeService_IsPrimeShould
139
-
{
140
-
[Fact]
141
-
publicvoidIsPrime_InputIs1_ReturnFalse()
142
-
{
143
-
varprimeService=newPrimeService();
144
-
boolresult=primeService.IsPrime(1);
145
-
146
-
Assert.False(result, "1 should not be prime");
147
-
}
148
-
}
149
-
}
150
-
```
131
+
namespacePrime.UnitTests.Services
132
+
{
133
+
publicclassPrimeService_IsPrimeShould
134
+
{
135
+
[Fact]
136
+
publicvoidIsPrime_InputIs1_ReturnFalse()
137
+
{
138
+
varprimeService=newPrimeService();
139
+
boolresult=primeService.IsPrime(1);
140
+
141
+
Assert.False(result, "1 should not be prime");
142
+
}
143
+
}
144
+
}
145
+
```
151
146
152
147
The `[Fact]` attribute declares a test method that's run by the test runner. From the *PrimeService.Tests* folder, run `dotnet test`. The [dotnet test](../tools/dotnet-test.md) command builds both projects and runs the tests. The xUnit test runner contains the program entry point to run the tests. `dotnet test` starts the test runner using the unit test project.
153
148
@@ -168,8 +163,7 @@ Run `dotnet test`. The test passes.
168
163
169
164
### Add more tests
170
165
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.
166
+
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. But don't do it, as there's a better way.
173
167
174
168
```csharp
175
169
varprimeService=newPrimeService();
@@ -180,10 +174,10 @@ Assert.False(result, "1 should not be prime");
180
174
181
175
Copying test code when only a parameter changes results in code duplication and test bloat. The following xUnit attributes enable writing a suite of similar tests:
182
176
183
-
-`[Theory]` represents a suite of tests that execute the same code but have different input arguments.
184
-
-`[InlineData]` attribute specifies values for those inputs.
177
+
*`[Theory]` represents a suite of tests that execute the same code but have different input arguments.
178
+
*`[InlineData]` attribute specifies values for those inputs.
185
179
186
-
Rather than creating new tests, apply the preceding xUnit attributes to create a single theory. Replace the following code:
180
+
Rather than creating new tests, apply the preceding xUnit attributes to create a single theory. Replace the following code...
187
181
188
182
```csharp
189
183
[Fact]
@@ -196,15 +190,15 @@ public void IsPrime_InputIs1_ReturnFalse()
0 commit comments