如何在NUnit TearDown中获取测试失败消息

本文关键字:获取 测试 失败 消息 TearDown NUnit | 更新日期: 2023-09-27 18:00:51

嗨,我想记录测试失败的消息,我知道可以在中检查状态

TearDown

使用以下

TestContext.CurrentContext.Result.Status.ToString();

可以在TearDown中获得测试失败的原因。有些东西像下面的

Expected: True
But was:  False
at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String message, Object[] args)
       at NUnit.Framework.Assert.IsTrue(Boolean condition)
       at Test.FileSystemTests.QuoteFiles() in ExampleTests.cs: line 57

如何在NUnit TearDown中获取测试失败消息

如果

TestContext.CurrentContext.Result 

不包含您要查找的值,您也可以尝试

TestExecutionContext.CurrentContext.CurrentResult

这对我在TearDown中使用NUnit 3.0以及其他方法都有效。

嗨,我不确定你是否得到了这个问题的解决方案。但这是我的答案。使用Nunit 3.0,您可以通过以下行实现这一点。。。

TestContext.CurrentContext.Result.Outcome.Status; (For test Execution Status)
TestContext.CurrentContext.Result.StackTrace; (For Stack Trace Message)
TestContext.CurrentContext.Result.Message; (For Test Result Message)

在拆卸中需要一个状态是测试气味的指示。拆卸应该在任何时候都可以工作,而不取决于单元测试的结果。我只是让您的系统处于与单元测试前相同的状态。你可以添加尝试,最后在你的眼泪中清理混乱。然而,依赖于一个断言(以及以后的更多断言(会使您的拆卸代码变得复杂,而这正是您现在在单元测试中想要的。

这个问题:NUnit:访问TearDown((中的失败消息可能会对您有所帮助。它的意图似乎足够相似。