代码之家  ›  专栏  ›  技术社区  ›  Dejan Scott Dorman

如何使用“dotnet测试”按类别筛选NUnit测试

  •  17
  • Dejan Scott Dorman  · 技术社区  · 7 年前

    我有一个项目

    [TestFixture, Category("Oracle")]
    

    和a

    [TestFixture, Category("OracleOdbc")]
    

    有几个我想执行的测试 分别地 使用 dotnet test .

    以下是我在谷歌搜索后尝试的内容:

    1. dotnet test MyProject.csproj --where "cat==Oracle" 但这个开关已经不存在了。
    2. dotnet test MyProject.csproj --filter Category="Oracle" 产生0个适用测试: No test is available in ... .

    然后,我绊倒了 this article 尽管它描述了MSTest(而NUnit有 CategoryAttribute 而不是 TestCategoryAttribute ),我试过了

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

    答对 了这次执行了所有“Oracle”测试。但现在是令人困惑的部分。如果我跑步 dotnet test MyProject.csproj --filter TestCategory="OracleOdbc" , 全部的 正在执行测试,包括“Oracle” “OracleOdbc”。这让我想知道 TestCategroy 是NUnit的正确方式,或者如果这是一个bug。

    我正在使用。NET命令行工具(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 .

    4 回复  |  直到 7 年前
        1
  •  15
  •   Douglas Waugh    6 年前

    这可能不是很有帮助,但它似乎对我来说是正确的。我使用dotnet cli创建了这些项目。

    首先,我安装了NUnit3测试适配器 instructions from here . 这只需要在每台机器上运行一次,所以如果已经运行了,就不需要再次运行。

    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 .

    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版

        2
  •  2
  •   M.Hassan    4 年前

    在Nunit框架中,类别属性可以位于方法级别。

    例子:

    public class NUnitTest 
        {
            [Test]
            [Category("CategoryA")] 
            public void TestMethod1()
            {
            }
    
            [Test]
            [Category("CategoryB")] 
            public void TestMethod2()
            {
            }
    
        }
    

    命令是:

    dotnet test --filter TestCategory=CategoryA     #Runs tests which are annotated with [Category("CategoryA")].
    

    此外,在方法和其他方面有许多选择 有关更多详细信息: read

        3
  •  0
  •   cbp    3 年前

    现在有两个选项可以使用 dotnet test . 您可以使用任何一种dotnet。exe的内置测试过滤语法,或NUnit过滤语法。

    首先,使用NuGet将NUnit3TestAdapter添加到项目中:

    install-package nunit3testadapter -proj YourProject
    

    然后可以按如下方式筛选测试:

    dotnet.exe test .\ --test-adapter-path:. --filter TestCategory=Foo
    

    或者像这样:

    dotnet.exe test .\ --test-adapter-path:. -- NUnit.Where="cat=Foo"
    

    This blog post 深入了解更多细节。

        4
  •  -1
  •   Tono Nam    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 ...
    
    }