Running tests automatically with dotnet test

Posted: (EET/GMT+2)

 

If you're using .NET Core or .NET 5, the dotnet test command is one of the simplest ways to automate your test runs. It works with xUnit, MSTest and NUnit, and integrates smoothly into your CI pipelines or simple batch or PowerShell scripts.

To run tests for a solution, just navigate to the folder and execute:

dotnet test

The results are shown in text on your console, with colors if the console/terminal supports them. You can also target a specific project:

dotnet test MyProject.Tests.csproj

For faster feedback loops, use the --no-build switch if you know the build is already up-to-date:

dotnet test --no-build

If you need test results in machine-readable format (such as for Azure DevOps or GitHub Actions), add:

dotnet test --logger "trx;LogFileName=test-results.trx"

This is a small tool, but it becomes powerful when you start automating builds and running tests on every commit. Being able to quickly launch your tests is great, too.

Happy unit testing!