The .NET Test SDK provides the infrastructure that connects .NET test projects to test execution. A typical test project combines it with a testing framework such as xUnit, NUnit, or MSTest, plus a runner adapter. section 35.0 section 35.1
A test project uses these pieces together:
- The .NET Test SDK marks the project as a test project by setting the IsTestProject MSBuild property. section 35.2
- A framework defines tests, such as xUnit's [Fact] and [Theory] methods. section 35.7
- A runner adapter lets dotnet test and Visual Studio discover and execute the framework's tests. section 35.1 section 35.3
Create and run a test project
Create an xUnit project with dotnet new xunit. The template adds Microsoft.NET.Test.Sdk, xunit, and xunit.runner.visualstudio packages. Run dotnet test from the project folder, or use Test Explorer in Visual Studio. The command restores and builds the project before running its tests. section 35.2 section 35.3
Listing 35.1 The .csproj file for an xUnit test project
<Project Sdk="Microsoft.NET.Sdk"> #1 <PropertyGroup> #1 <TargetFramework>net7.0</TargetFramework> #1 <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> #2 <PackageReference Include="xunit" Version="2.4.2" /> #3 <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" /> #4 <PackageReference Include="coverlet.collector" Version="3.1.2" /> #5 </ItemGroup> </Project> #1 The test project is a standard .NET 7.0 project. #2 The .NET Test SDK, required by all test projects #3 The xUnit test framework #4 The xUnit test adapter for the .NET Test SDK #5 An optional package that collects metrics about how much of your code base is covered by tests
Inspect the project file to see how the .NET Test SDK, xUnit framework, runner adapter, and optional coverage collector are declared as NuGet packages. section 35.2
