【问题标题】:Can NUnit Console Runner report failed tests on the fly?NUnit Console Runner 可以即时报告失败的测试吗?
【发布时间】:2014-10-03 14:38:39
【问题描述】:

nunit-console 似乎是这样工作的(参数:/nologo /wait /labels ...)

当一个测试开始时它只输出每个测试的标签,像这样:

***** Our.Tests.Bla
... here come some Console.WriteLine
// Note that in case Our.Tests.Bla fails, nothing is reported here immediately
***** Our.Tests.Blub
... here come some Console.WriteLine
***** Our.Tests.Foo
... here come some Console.WriteLine

如果任何测试失败,它只会在整个运行结束时报告测试失败。

通常这很好,但我们也通过 NUnit 运行一些相互依赖的集成测试,有时一个测试挂起,因为之前的测试失败了。

问题是,当一个测试挂起时,您看不到哪个/如果有任何先前的测试失败,这使得快速追踪问题变得非常困难。 (特别是当测试挂在测试服务器机器上时,或者您可能只有一个中止运行的日志。)

我真的更希望 让 NUnit 在开始下一个测试之前即时/立即报告失败的测试,包括详细的错误。这可能吗?

【问题讨论】:

    标签: c# nunit nunit-console


    【解决方案1】:

    您可以这样做的一种方法是在测试文件中包含 Teardown,该文件在每次测试完成时运行,然后可以在测试失败时输出测试的名称。

        [TearDown]
        public void TearDown()
        {
            var status = TestContext.CurrentContext.Result.Status;
            if(status != TestStatus.Passed)
              Console.WriteLine("Test Failed: {0}", TestContext.CurrentContext.Test.FullName);   
        }
    

    【讨论】:

    • 好的。这似乎可以很好地管理。好主意,谢谢。
    【解决方案2】:

    Another question on here asked about having the console runner halt on the first error that it encountered.

    /stoponerror 选项不是您想要的,但它可能会和您得到的一样好。

    【讨论】:

    • stoponerror 只能在每个 nunit-console 调用中定义,对吧?
    【解决方案3】:

    此答案基于Andrew's answer

    一些类在 NUnit 3 中改变了,所以TearDown 方法也需要改变。结果如下所示:

    [TearDown]
    public void TearDown()
    {
        var status = TestContext.CurrentContext.Result.FailCount;
        if (status > 0)
            Console.WriteLine("Test Failed: {0}\r\nMessage:\r\n{1}", 
                               TestContext.CurrentContext.Test.FullName, 
                               TestContext.CurrentContext.Result.Message);          
    } 
    

    【讨论】:

      【解决方案4】:

      您可以使用 /labels。这至少可以帮助您找到失败的测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-20
        • 2018-03-04
        • 2018-12-17
        • 1970-01-01
        • 2018-04-05
        • 1970-01-01
        相关资源
        最近更新 更多