liveBook · Manning

Dotnet test

As explained in .NET in Action, Second Edition

dotnet test is a .NET CLI command that builds a test project, discovers its tests, and executes them.

`dotnet test` runs tests from a .NET project. From the test project’s directory, it finds the project file, discovers its tests, and executes them. Use it after creating an xUnit test project and adding a reference to the code under test. section 6.4

Listing 6.8 Commands to create Sudoku projects
dotnet new classlib --name SudokuSolver
dotnet new xunit --name SudokuSolver.UnitTests             #1
cd SudokuSolver.UnitTests
dotnet add reference ../SudokuSolver/SudokuSolver.csproj   #2

Create a class library and an xUnit project, then reference the library from the test project. Run `dotnet test` from the test project’s directory. section 6.4

Run a Fact test

A Sudoku test constructs a 4×4 board and checks `Assert.True(solver.IsValid())` inside a `[Fact]` method. A second test uses `Assert.False` for a nonsquare board. Run `dotnet test` in the unit-test project’s folder to execute them. section 6.5

Listing 6.11 Empty 4x4 board test output
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.           #1
  SudokuSolver.UnitTests.SolverTests.Empty4x4Board [FAIL]        #2
  Failed SudokuSolver.UnitTests.SolverTests.Empty4x4Board [6 ms]
  Error Message:
   Assert.True() Failure                                         #3
Expected: True
Actual:   False
  Stack Trace:
     at SudokuSolver.UnitTests.SolverTests.Empty4x4Board() in
     /ch06/SudokuSolver.UnitTests/SolverTests.cs:line 12         #4

The empty-board test fails while `IsValid()` is stubbed to return `false`. The output names the failed test, shows the expected and actual values, and points to the assertion’s source line. section 6.5

The same command checks an ASP.NET Core test project after its scaffolding is in place. Here, a test client requests a controller endpoint and asserts that the response status is OK. section 12.1

Listing 12.6 Tests for MeasureController
using Microsoft.AspNetCore.Mvc.Testing;              #1
using static System.Net.HttpStatusCode;              #2
  
namespace AcController.Tests;
  
public class MeasureTests
{
  private readonly WebApplicationFactory<Program>    #3
    _factory;
  
  private readonly HttpClient _client;               #4
  
  public MeasureTests()
  {
    _factory = new WebApplicationFactory<Program>();
    _client = _factory.CreateClient();
  }
  
  [Fact]
  public async void RespondsOK()
  {
    var response = await _client.GetAsync(
      "/measure/TIJ1/1");                            #5
    Assert.Equal(OK, response.StatusCode);
  }
}

This xUnit test uses `WebApplicationFactory<Program>` to create an HTTP client, request `/measure/TIJ1/1`, and check the status code. Run `dotnet test` to check that the test works. section 12.1

How .NET in Action, Second Edition covers it

How deep it goes
Shows project setup, test execution, and failure output, but gives no command-option reference.
Examples
Uses Sudoku board-validity assertions and an HTTP OK response test against a controller.
What it assumes
Assumes basic C# project structure and assertions while showing how to create an xUnit project and run its tests.
What you can do after
Operate/configure it to run xUnit projects and use assertion failures to locate a failing test.
How it connects
Connects xUnit Fact tests and assertions with project references and ASP.NET Core integration testing.

Also explained in

Compare how 2 books explain dotnet test →

Related concepts

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