【问题标题】:How to filter NUnit tests by category using "dotnet test"如何使用“dotnet test”按类别过滤 NUnit 测试
【发布时间】:2018-01-10 20:51:18
【问题描述】:

我有一个项目有一个

[TestFixture, Category("Oracle")]

还有一个

[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 的正确方法,或者这是否是一个错误。

我正在使用 .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.0net462

【问题讨论】:

  • 我在这里观察到的一件事是用于类别比较的单 = 符号。我使用 nunit 从命令行运行我的测试,它们看起来像这样:nunit3-console.exe MyTestProject.dll --where "cat==myCategory"
  • @Freya:感谢您的发现。我已经修好了。然而,这个问题是关于dotnet test。您正在直接启动nunit3-console.exe

标签: c# .net-core nunit dotnet-cli


【解决方案1】:

这可能不是很有帮助,但它似乎对我来说是正确的。我使用 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 版

【讨论】:

  • 你使用的是哪个NUnit 版本?
  • NUnit 版本也是 3.9.0
  • 如何指定多个TestCategory?是否可以结合 Oracle 和 OracleOdbc? dotnet test --no-build --filter "TestCategory=Oracle&TestCategory=OracleOdbc" GherkinSuites.csproj
【解决方案2】:

在 Nunit 框架中,Category Attribute 可以在 Method 级别。

示例:

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】:

    现在有两个选项可以使用 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】:

      如果你喜欢像我这样的枚举,你也可以在你的测试中放置一个过滤器:

          [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 ...
      
      }
      

      【讨论】:

      • 就我个人而言,我不喜欢将实际测试代码与分组和过滤它们的问题混为一谈(恕我直言,这是一个外部问题)。
      猜你喜欢
      • 2022-11-21
      • 1970-01-01
      • 2017-07-04
      • 1970-01-01
      • 2015-02-16
      • 2016-10-11
      • 1970-01-01
      • 2021-04-21
      相关资源
      最近更新 更多