LinuxCommandLibrary

dotnet-test

Run .NET unit tests

TLDR

Execute tests for a .NET project/solution in the current directory

$ dotnet test
copy

Execute tests for a .NET project/solution in a specific location
$ dotnet test [path/to/project_or_solution]
copy

Execute tests matching the given filter expression
$ dotnet test --filter [Name~TestMethod1]
copy

SYNOPSIS

dotnet test [options] [[--] <project.dll> [<project.dll>...]]

PARAMETERS

--blame
    Runs tests with blame mode for crash/hang diagnosis.

--blame-crash-reply-file <FILE>
    File for crash dump collection instructions.

--blame-hang-timeout <TIMEOUT>
    Timeout in seconds for hang detection (default: 30s).

--collect:"<DATA_COLLECTOR>[;logDirectory=...]"
    Enables data collectors like Code Coverage.

--configuration <CONFIGURATION>
    Build configuration (Debug/Release).

--diag <FILE>
    Enables verbose diagnostic logging to file.

--filter <EXPRESSION>
    Runs tests matching filter (e.g., FullyQualifiedName~TestMethod).

--framework <FRAMEWORK>
    Target .NET framework (e.g., net6.0).

--logger <LOGGER>
    Configures loggers (e.g., trx;LogFileName=... ).

--list-tests [<PROJECT>]
    Lists tests without running them.

--list-test-targets [<PROJECT>]
    Lists test targets.

--no-build
    Skips project build before testing.

--no-restore
    Skips restore before build.

--results-directory <DIR>
    Output directory for test results.

--runtime <RUNTIME_ID>
    Target runtime identifier (e.g., linux-x64).

--settings <FILE>
    runsettings file for test parameters.

--verbosity <LEVEL> (-v)
    Sets output verbosity (q[uiet]/m[inimal]/n[ormal]/d[etailed]/diag).

-h|--help
    Shows command help.

-- <ARGS>
    Passes arguments to test host.

DESCRIPTION

The dotnet test command is a key part of the .NET CLI toolchain, designed to discover, run, and report on unit tests in .NET projects. It supports popular frameworks like xUnit, NUnit, and MSTest, automatically detecting test projects via ProjectReference or direct assembly paths.

On Linux, it leverages the cross-platform dotnet runtime, executing tests in-process or out-of-process with the VSTest console runner. Tests run in parallel by default across CPU cores for efficiency, with options to control parallelism, logging, and data collection.

Key features include filtering tests via traits or names, code coverage via tools like Coverlet, blame mode for hang/crash diagnosis, and HTML/XML logging. It builds projects in Debug/Release modes, respecting global.json and MSBuild properties. Ideal for CI/CD pipelines on Linux servers with tools like GitHub Actions or Azure DevOps.

Output includes pass/fail summaries, timings, and failures with stack traces. Customizable via runsettings files for advanced scenarios like test timeouts or environment variables.

CAVEATS

Requires .NET SDK installed; test projects must reference a test framework package. Parallel execution may cause intermittent issues with shared state. Blame mode increases overhead. Not for integration tests needing full app hosting (use dotnet run). Linux requires compatible runtimes for self-contained tests.

EXAMPLES

Basic: dotnet test
With coverage: dotnet test --collect:"XPlat Code Coverage"
Filter: dotnet test --filter "Category=Integration"
No build: dotnet test --no-build

TEST ADAPTERS

Supports custom options prefixed with framework name, e.g., -- NUnit.MaxTestLevel=3 or -- MSTestV2.TestTimeout=1000 for timeouts.

HISTORY

Introduced in .NET Core 1.0 (2016) as part of dotnet CLI for cross-platform testing. Evolved in .NET Core 2.0+ with parallel execution, blame mode (3.0), and improved filtering/coverage (5.0+). Unified in .NET 6+ (2021) with single-file publishing support. Actively maintained by Microsoft for .NET 8+.

SEE ALSO

dotnet(1), dotnet-build(1), dotnet-run(1), dotnet-vstest(1), vstest.console(1)

Copied to clipboard