C# NUnit控制台运行程序能否动态报告失败的测试?
C# NUnit控制台运行程序能否动态报告失败的测试?,c#,nunit,nunit-console,C#,Nunit,Nunit Console,nunit控制台似乎是这样工作的(参数:/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
nunit控制台
似乎是这样工作的(参数:/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在开始下一个测试之前立即报告失败的测试,包括详细的错误。这可能吗?
/stoponerror
选项不是您所要求的,但它可能与您所能得到的一样好。一种方法是在您的测试文件中有一个拆卸,它在每个测试完成时运行,如果失败,它可以输出测试的名称
[TearDown]
public void TearDown()
{
var status = TestContext.CurrentContext.Result.Status;
if(status != TestStatus.Passed)
Console.WriteLine("Test Failed: {0}", TestContext.CurrentContext.Test.FullName);
}
这个答案是基于
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);
}
您可以使用/标签。这至少可以帮助您定位失败的测试。stoponerror只能在每次nunit控制台调用时定义,对吗?确定。这似乎有点得体。好主意,谢谢。