Skip to content

Commit 7e7a354

Browse files
authored
Update unit testing properties (#45767)
1 parent 90fab68 commit 7e7a354

File tree

3 files changed

+79
-63
lines changed

3 files changed

+79
-63
lines changed

docs/core/project-sdk/msbuild-props.md

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,34 +1463,24 @@ The allowed values of this property are SDK feature bands, for example, 8.0.100
14631463

14641464
For more information, see [SDK Analysis Level Property and Usage](https://github.com/dotnet/designs/blob/main/proposed/sdk-analysis-level.md).
14651465

1466-
## Test project–related properties
1466+
## Microsoft.Testing.Platform–related properties
14671467

14681468
The following MSBuild properties are documented in this section:
14691469

1470-
- [IsTestProject](#istestproject)
14711470
- [IsTestingPlatformApplication](#istestingplatformapplication)
14721471
- [Enable\[NugetPackageNameWithoutDots\]](#enablenugetpackagenamewithoutdots)
14731472
- [EnableAspireTesting](#enableaspiretesting)
1474-
- [EnablePlaywright](#enableplaywright)
14751473
- [EnableMSTestRunner](#enablemstestrunner)
14761474
- [EnableNUnitRunner](#enablenunitrunner)
1475+
- [EnablePlaywright](#enableplaywright)
1476+
- [GenerateTestingPlatformConfigurationFile](#generatetestingplatformconfigurationfile)
14771477
- [GenerateTestingPlatformEntryPoint](#generatetestingplatformentrypoint)
1478+
- [TestingExtensionsProfile](#testingextensionsprofile)
14781479
- [TestingPlatformCaptureOutput](#testingplatformcaptureoutput)
14791480
- [TestingPlatformCommandLineArguments](#testingplatformcommandlinearguments)
14801481
- [TestingPlatformDotnetTestSupport](#testingplatformdotnettestsupport)
14811482
- [TestingPlatformShowTestsFailure](#testingplatformshowtestsfailure)
1482-
- [TestingExtensionsProfile](#testingextensionsprofile)
1483-
- [UseVSTest](#usevstest)
1484-
- [MSTestAnalysisMode](#mstestanalysismode)
1485-
1486-
### IsTestProject
1487-
1488-
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.
1483+
- [UseMicrosoftTestingPlatformRunner](#usemicrosofttestingplatformrunner)
14941484

14951485
### IsTestingPlatformApplication
14961486

@@ -1535,6 +1525,10 @@ The `EnableMSTestRunner` property enables or disables the use of the [MSTest run
15351525

15361526
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.
15371527

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+
15381532
### GenerateTestingPlatformEntryPoint
15391533

15401534
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
15431537

15441538
To control the generation of the entry point in a VSTest project, use the `GenerateProgramFile` property.
15451539

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+
15461544
### TestingPlatformCaptureOutput
15471545

15481546
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
15621560

15631561
### TestingPlatformDotnetTestSupport
15641562

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`.
15661564

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).
15681569

15691570
### TestingPlatformShowTestsFailure
15701571

@@ -1582,10 +1583,30 @@ When you use the [MSTest project SDK](../testing/unit-testing-mstest-sdk.md), th
15821583

15831584
For more information, see [Microsoft.Testing.Platform profile](../testing/unit-testing-mstest-sdk.md#microsofttestingplatform-profile).
15841585

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+
15851600
### UseVSTest
15861601

15871602
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).
15881603

1604+
## MSTest–related properties
1605+
1606+
The following MSBuild properties are documented in this section:
1607+
1608+
- [MSTestAnalysisMode](#mstestanalysismode)
1609+
15891610
### MSTestAnalysisMode
15901611

15911612
This property decides which analyzers are enabled at which severity. For more information, see [MSTest code analysis](../testing/mstest-analyzers/overview.md).

docs/core/testing/unit-testing-csharp-with-xunit.md

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.date: 03/07/2024
77
---
88
# Unit testing C# in .NET using dotnet test and xUnit
99

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).
1111

1212
## Create the solution
1313

@@ -60,11 +60,7 @@ The following instructions provide the steps to create the test solution. See [C
6060
}
6161
```
6262

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.
6864

6965
* In the *unit-testing-using-dotnet-test* directory, run the following command to add the class library project to the solution:
7066

@@ -78,13 +74,12 @@ The following instructions provide the steps to create the test solution. See [C
7874
dotnet new xunit -o PrimeService.Tests
7975
```
8076

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`
8883

8984
* Add the test project to the solution file by running the following command:
9085

@@ -121,33 +116,33 @@ Follow the instructions for "Replace the code in *PrimeService.cs* with the foll
121116

122117
## Create a test
123118

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.
125120

126121
Update the *PrimeService.Tests* project:
127122

128123
* Delete *PrimeService.Tests/UnitTest1.cs*.
129124
* Create a *PrimeService.Tests/PrimeService_IsPrimeShould.cs* file.
130125
* Replace the code in *PrimeService_IsPrimeShould.cs* with the following code:
131126

132-
```csharp
133-
using Xunit;
134-
using Prime.Services;
127+
```csharp
128+
using Xunit;
129+
using Prime.Services;
135130

136-
namespace Prime.UnitTests.Services
137-
{
138-
public class PrimeService_IsPrimeShould
139-
{
140-
[Fact]
141-
public void IsPrime_InputIs1_ReturnFalse()
142-
{
143-
var primeService = new PrimeService();
144-
bool result = primeService.IsPrime(1);
145-
146-
Assert.False(result, "1 should not be prime");
147-
}
148-
}
149-
}
150-
```
131+
namespace Prime.UnitTests.Services
132+
{
133+
public class PrimeService_IsPrimeShould
134+
{
135+
[Fact]
136+
public void IsPrime_InputIs1_ReturnFalse()
137+
{
138+
var primeService = new PrimeService();
139+
bool result = primeService.IsPrime(1);
140+
141+
Assert.False(result, "1 should not be prime");
142+
}
143+
}
144+
}
145+
```
151146

152147
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.
153148

@@ -168,8 +163,7 @@ Run `dotnet test`. The test passes.
168163

169164
### Add more tests
170165

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.
173167

174168
```csharp
175169
var primeService = new PrimeService();
@@ -180,10 +174,10 @@ Assert.False(result, "1 should not be prime");
180174

181175
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:
182176

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.
185179

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...
187181

188182
```csharp
189183
[Fact]
@@ -196,15 +190,15 @@ public void IsPrime_InputIs1_ReturnFalse()
196190
}
197191
```
198192

199-
with the following code:
193+
...with the following code:
200194

201-
:::code language="csharp" source="../../../samples/snippets/core/testing/unit-testing-using-dotnet-test/csharp/PrimeService.Tests/PrimeService_IsPrimeShould.cs" id="Sample_TestCode":::
195+
:::code language="csharp" source="../../../samples/snippets/core/testing/unit-testing-using-dotnet-test/csharp/PrimeService.Tests/PrimeService_IsPrimeShould.cs" id="Sample_TestCode":::
202196

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

205199
Add the following code after the class declaration and before the `[Theory]` attribute:
206200

207-
:::code language="csharp" source="../../../samples/snippets/core/testing/unit-testing-using-dotnet-test/csharp/PrimeService.Tests/PrimeService_IsPrimeShould.cs" id="Sample_InitCode":::
201+
:::code language="csharp" source="../../../samples/snippets/core/testing/unit-testing-using-dotnet-test/csharp/PrimeService.Tests/PrimeService_IsPrimeShould.cs" id="Sample_InitCode":::
208202

209203
Run `dotnet test`, and two of the tests fail. To make all of the tests pass, update the `IsPrime` method with the following code:
210204

@@ -225,6 +219,6 @@ The completed `IsPrime` method is not an efficient algorithm for testing primali
225219

226220
### Additional resources
227221

228-
- [xUnit.net official site](https://xunit.net)
229-
- [Testing controller logic in ASP.NET Core](/aspnet/core/mvc/controllers/testing)
230-
- [`dotnet reference add`](../tools/dotnet-reference-add.md)
222+
* [xUnit.net official site](https://xunit.net)
223+
* [Testing controller logic in ASP.NET Core](/aspnet/core/mvc/controllers/testing)
224+
* [`dotnet reference add`](../tools/dotnet-reference-add.md)

docs/core/testing/unit-testing-mstest-sdk.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ You can set the profile using the property `TestingExtensionsProfile` with one o
8989
* [Code Coverage](./microsoft-testing-platform-extensions-code-coverage.md#microsoft-code-coverage)
9090

9191
* [Trx Report](./microsoft-testing-platform-extensions-test-reports.md#visual-studio-test-reports)
92-
92+
9393
* `AllMicrosoft` - Enable all extensions shipped by Microsoft (including extensions with a restrictive license).
9494

9595
Enables the following extensions:
@@ -105,7 +105,7 @@ You can set the profile using the property `TestingExtensionsProfile` with one o
105105
* [Hot Reload](./microsoft-testing-platform-extensions-hosting.md#hot-reload)
106106

107107
* [Retry](./microsoft-testing-platform-extensions-policy.md#retry)
108-
108+
109109
* [Trx Report](./microsoft-testing-platform-extensions-test-reports.md#visual-studio-test-reports)
110110

111111
Here's a full example, using the `None` profile:
@@ -285,4 +285,5 @@ The NuGet-provided MSBuild SDKs (including MSTest.Sdk) have limited tooling supp
285285
286286
## See also
287287

288-
- [Test project&ndash;related properties](../project-sdk/msbuild-props.md#test-projectrelated-properties)
288+
- [Microsoft.Testing.Platform&ndash;related properties](../project-sdk/msbuild-props.md#microsofttestingplatformrelated-properties)
289+
- [VSTest&ndash;related properties](../project-sdk/msbuild-props.md#vstestrelated-properties)

0 commit comments

Comments
 (0)