【问题标题】:How to categorize test classes and test methods and select specific categories for testing in nunit?在 nunit 中如何对测试类和测试方法进行分类并选择特定的类别进行测试?
【发布时间】:2017-12-25 18:35:01
【问题描述】:

是否可以对测试类或测试方法进行分类,例如在 nunit 中说为 BASIC、PERFORMANCE 和 SIMPLE,然后允许用户决定测试的类别他想跑。 Can the nunit extensions help in this in anyway? 一个例子的解释将是非常可观的。 PS:我目前使用的是 nunit 2.6.4

【问题讨论】:

  • 您可以将CategoryAttribute 用于类或方法。然后根据测试运行器,您将能够执行特定类别的测试
  • @Fabio 你能分享一个同样的例子吗?我目前正在使用 nunit 2.6.4 作为测试运行器。
  • 测试运行者是指他们的 GUI。例如 Resharper 和 Test Explorer 具有通过CategoryAttribute 执行测试的功能。如果您想为实际的测试运行者实现这一点 - 那么在不同的类/文件/项目中分离不同的类别将有可能通过命令行分别执行它们

标签: c# unit-testing visual-studio-2015 automated-tests nunit


【解决方案1】:

是的,您可以按类或测试级别对测试进行分类,测试也可以有多个类别。

上面提到的一个选项是[TestCategory("MyCategory")] 路由,但我更喜欢只创建新属性,这样我就可以有一组更“强”类型的类别可供选择。

例子:

public class FastIntegrationTestAttribute : CategoryAttribute { }

public class LongRunningIntegrationTestAttribute : CategoryAttribute { }

public class NightlyTestAttribute : CategoryAttribute { }

在行动:

[TestFixture, FastIntegrationTest]
public class MyClassThatNeedsTesting
{

    [Test]
    [NightlyTest]
    public void MyTest()
    {
    }

    [Test]
    public void AnotherTest()
    {
    }
}

请注意,fixture 级别属性将该属性应用于所有测试,因此MyTest 具有“FastIntegrationTest”和“NightlyTest”属性,而AnotherTest 仅具有“FastIntegrationTest”属性。

有多种方法可以运行特定类别的测试,具体取决于您的运行器和框架版本。

以dotnetcore为例,可以这样做:

dotnet test --where "cat == NightlyTest"

除了@Charlie 提到的方法之外,resharper 测试运行器还允许按类别分组

【讨论】:

    【解决方案2】:

    您使用CategoryAttribute 将类别应用于测试或夹具。

    然后,您可以根据您使用的跑步者以各种方式运行它们。比如……

    • NUnit 2.6.4 控制台运行程序 - 在命令行上使用 /include 和/或 /exclude 选项。

    • NUnit 2.6.4 GUI 运行器 - 选择要在 gui 中运行的类别

    • 在 Visual Studio 上运行的 NUnit VS 适配器 - 按特征对测试进行排序并选择要运行的类别。

    【讨论】:

    • 我尝试根据proceduregiven在我的测试中添加测试类别属性。但随后它说“找不到类型或命名空间 TestCategoryAttribute 是否缺少 using 指令或程序集引用”。请帮助。这是我的代码 sn-p:[TestCategory("Nightly")] [Test] public void positivetest() { int x = 7;整数 y = 7;断言.AreEqual(x, y); }
    • 您正在使用 NUnit,但您正在查看的文档适用于 MSTest。 NUnit 有 CategoryAttribute 而不是 TestCategoryAttribute。见github.com/nunit/docs/wiki/Category-Attribute
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多