How to filter NUnit tests by category using "dotnet test"
This might not be very helpful, but it seems to be working for me correctly. I created the projects using the dotnet-cli.
First I installed the NUnit3 test adapter instructions from here. This only needs to be run once on each machine so you don't need to do it again if you have already run it.
dotnet new -i NUnit3.DotNetNew.Template
Then I created my solution, created my test project and added the test project to the solution.
dotnet new sln -n Solution
dotnet new nunit -n TestProject -o tests\TestProject
dotnet sln add tests\TestProject\TestProject.csproj
Then I updated UnitTest1.cs to include two test fixtures, one with the category Oracle
and one with the category OracleOdbc
.
using NUnit.Framework;
namespace Tests
{
[TestFixture]
[Category("Oracle")]
public class OracleTests
{
[Test]
public void OracleTest()
{
Assert.Fail();
}
}
[TestFixture]
[Category("OracleOdbc")]
public class OracleOdbcTests
{
[Test]
public void OracleOdbcTest()
{
Assert.Fail();
}
}
}
Then I can specify which category I choose to run.
dotnet test tests/TestProject/TestProject.csproj --filter TestCategory="Oracle"
or
dotnet test tests/TestProject/TestProject.csproj --filter TestCategory="OracleOdbc"
both run only one test and the message shows it is the correct test that fails.
Using DotNet-Cli version 2.1.4 and NUnit3TestAdapter version 3.9.0
In Nunit Framework, Category Attribute can be on the level of the Method.
Example:
public class NUnitTest
{
[Test]
[Category("CategoryA")]
public void TestMethod1()
{
}
[Test]
[Category("CategoryB")]
public void TestMethod2()
{
}
}
and command is:
dotnet test --filter TestCategory=CategoryA #Runs tests which are annotated with [Category("CategoryA")].
Also, there are many options on the level of method and others For more details: read