【问题标题】:Nunit: Add Category to specific test casesNunit:将类别添加到特定的测试用例
【发布时间】:2015-12-14 10:08:56
【问题描述】:

我想运行一小组 NUnit 测试用例作为检查前的健全性检查,并在我的检查时和夜间测试运行中运行一组更全面的测试用例。

所以我希望我可以用“Category”属性装饰某些测试用例,并且只有那些测试用例在预检时运行。但是,这似乎不起作用 - 如果我包含该类别,那么所有测试用例都会运行。

有没有办法限制通过类别运行的测试用例的数量?

[TestFixture]
public class TestAddition
{
    [TestCase(1, 2, 3), Category("PreCheckin")]
    [TestCase(2, 4, 6)]
    [TestCase(3, 6, 9)]
    public void AdditionPassTest(int first, int second, int expected)
    {
        var adder = new Addition();
        var total = adder.DoAdd(first, second);
        Assert.AreEqual(expected, total);
    }
}

如果我尝试运行它:

C:\> "C:\Program files (x86)\Nunit 2.6.4\bin\nunit-console.exe" /nologo ^
     NUnitTestCase.dll /labels /include=PreCheckin
ProcessModel: Default    DomainUsage: Single
Execution Runtime: net-3.5
Included categories: PreCheckin
***** NUnitTestCase.TestAddition.AdditionPassTest(1,2,3)
***** NUnitTestCase.TestAddition.AdditionPassTest(2,4,6)
***** NUnitTestCase.TestAddition.AdditionPassTest(3,6,9)

Tests run: 3, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0.0743007328107035 seconds
Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

我只想运行单个测试用例(1、2、3)

【问题讨论】:

    标签: c# nunit testcase


    【解决方案1】:

    您现在对所有测试都使用 Category 属性。将代码更改为这个:)

    [TestFixture]
    public class TestAddition
    {
        [TestCase(1, 2, 3, Category = "PreCheckin")]
        [TestCase(2, 4, 6)]
        [TestCase(3, 6, 9)]
        public void AdditionPassTest(int first, int second, int expected)
        {
            var adder = new Addition();
            var total = adder.DoAdd(first, second);
            Assert.AreEqual(expected, total);
        }
    }
    

    【讨论】:

    • 我注意到如果您从 ReSharper“单元测试资源管理器”运行“”测试,它将运行上述所有三个测试用例。
    • [TestCase(1, 2, 3, Category = "PreCheckin")] ins's not there a bracket missing (1,2,3), Category
    猜你喜欢
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多