如何使用“dotnet test”按类别过滤NUnit测试

问题描述 投票:6回答:1

我有一个项目有一个

[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,或者这是一个错误。

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

c# .net-core nunit dotnet-cli
1个回答
4
投票

这可能不是很有帮助,但似乎正确地为我工作。我使用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

© www.soinside.com 2019 - 2024. All rights reserved.