liveBook · Manning

Testing SDK

As explained in ASP.NET Core in Action, 3e

.NET Test SDK is the .NET SDK component that provides the test-running infrastructure and integration required by .NET test projects.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines project components and execution flow, but defers framework-specific test syntax and advanced testing practices.
Examples
Uses an xUnit project file, dotnet new xunit, dotnet test, Test Explorer, and a template-generated [Fact] test.
What it assumes
Starts with standard .NET project structure and introduces testing frameworks, package dependencies, test projects, and test execution.
What you can do after
Apply it via dotnet new xunit, dotnet test, or Visual Studio to create and execute .NET test projects.
How it connects
Ties test projects to xUnit, NUnit, MSTest, test-runner adapters, Visual Studio, Test Explorer, and the .NET CLI.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.