How to filter NUnit tests by category using "dotnet test"

I have a project that has

[TestFixture, Category("Oracle")]

and

[TestFixture, Category("OracleOdbc")]

with a couple of tests that I would like to run separately with dotnet test.

Here is what I tried after doing some googling:

  1. dotnet test MyProject.csproj --where "cat==Oracle" but this switch no longer exists.
  2. dotnet test MyProject.csproj --filter Category="Oracle"0 gives the applicable tests: No test is available in....

Then I stumbled upon this article and, although it describes MSTest (and NUnit has TestCategoryAttribute CategoryAttributeand not TestCategoryAttribute), I tried

  1. dotnet test MyProject.csproj --filter TestCategory="Oracle"

. "Oracle" . . dotnet test MyProject.csproj --filter TestCategory="OracleOdbc", dotnet test MyProject.csproj --filter TestCategory="OracleOdbc" , "Oracle" "OracleOdbc". , TestCategroy NUnit .

.NET Command Line Tools (2.1.2) :

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="NUnit" Version="3.8.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.7" />

, , , - netcoreapp2.0 net462.

+8
2

, , , . , dotnet-cli.

NUnit3 . , , .

dotnet new -i NUnit3.DotNetNew.Template

, .

dotnet new sln -n Solution
dotnet new nunit -n TestProject -o tests\TestProject
dotnet sln add tests\TestProject\TestProject.csproj

UnitTest1.cs, : Oracle OracleOdbc 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();
        }
    }
}

, .

dotnet test tests/TestProject/TestProject.csproj --filter TestCategory="Oracle"

dotnet test tests/TestProject/TestProject.csproj --filter TestCategory="OracleOdbc"

, , , .

DotNet-Cli 2.1.4 NUnit3TestAdapter 3.9.0

+5

, :

    [Test]
    public void Test_Foo()
    {            
        // filter test
        switch (GlobalDefinitions.Category)
        {
                // OK Test
                case Category.Oracle:
                case Category.SQL:
                    break;

                // Do NOT test
                case Category.MongoDb:
                    Assert.Inconclusive();

                // Error
                default:
                    Assert.Fail("Not implemented case");
        }

        // perform test...

    }

GlobalDefinitions.Category , .


Flags

[Flags] // <-------------------- Important to shorten code 
public enum Category: int
{
    None = 0,
    Oracle = 1 << 0,
    SQL = 1 << 1,
    MongoDb = 1 << 2,

    // future categories        

    ALL = -1
}

//

public static void Filter(Category category)
{
    if(GlobalDefinitions.Category.HasFlag(category) == false)
       Assert.Inconclusive(); // do not perform test

    // else perform test                         
}

// :

[Test]
public void Test_Foo()
{ 
    Filter(Category.SQL | Category.MongoDb); // place filter (in this test we are testing for SQL and MongoDb

    // perform your test ...

}
0

Source: https://habr.com/ru/post/1692007/


All Articles