Quick example showing how to exclude tests in a certain project when running dotnet test.

The problem

I have a solution that contains three different test projects:

  • MyProject.Core.Tests
  • MyProject.Persistence.Tests
  • MyProject.IntegrationTests

When running dotnet test on our build server I don't want to run the tests in MyProject.IntegrationTests since they require a real database and I don't have access to one when running on the build server. I will run them later on in my docker swarm.

The solution

dotnet test --filter to the rescue!
On the following link you can find some examples on how to use the dotnet test --filter command.

They don't have any examples of how to exclude projects but they show you how to Runs tests which has TestClass1 in FullyQualifiedName or Category is CategoryA like this: dotnet test --filter "FullyQualifiedName~TestClass1|Category=CategoryA

So, can we somehow do the reverse? Like run all the tests except Tests that contains IntegrationTests in it's namespace?

Yes we can :)

dotnet test --filter FullyQualifiedName!~IntegrationTests

Great success!

Now when running dotnet test it will scan all of your test projects but exclude any tests that contains IntegrationTests in it's FullyQualifiedName.
It will also print out a warning to the console telling you that it could not find any tests in your IntegrationTest projects like this: No test matches the given testcase filter FullyQualifiedName!~IntegrationTests in... (given that you don't have any other tests in the project with a FQN that doesn't contain IntegrationTests of course)