简体   繁体   English

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

[英]how to get test fail message in NUnit TearDown

Hi i would like to log the test failed message , I know its possible to check the status in 嗨,我想记录测试失败的消息,我知道可以检查状态

TearDown

Using the following 使用以下内容

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

Is possible to get the reason for test fail in TearDown . 可以在TearDown中获得测试失败的原因。 Some thing like the following 有些事情如下

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

If 如果

TestContext.CurrentContext.Result 

does not contain the values you are looking for, you can also try 不包含您要查找的值,您也可以尝试

TestExecutionContext.CurrentContext.CurrentResult

This worked for me with NUnit 3.0 in the TearDown as well as other methods. 这对我来说在TearDown中使用NUnit 3.0以及其他方法。

The need for a status in your teardown is an indication of a testsmell. 在拆解中需要一个状态是一种测试的标志。 The teardown should work any time without depending on the result of the unit test. 拆卸应该可以随时工作,而不依赖于单元测试的结果。 I just brings your system in the same state as before your unit test. 我只是将您的系统置于与单元测试之前相同的状态。 You can add tries and finally to your teardown to clean the mess. 你可以添加尝试,最后到你的拆解来清理混乱。 However depending on one assert (and later on more) will make your teardown code complex and that is something your do now want in an unit test. 但是,根据一个断言(以及稍后的断言)将使您的拆卸代码变得复杂,这是您现在在单元测试中所需要的。

This question: NUnit: Accessing the Failure Message in TearDown() could potentially help you. 这个问题: NUnit:在TearDown()中访问失败消息可能对您有所帮助。 It seems similar enough in intent. 在意图上似乎足够相似。

Hi am not sure you got the solution for this issue. 嗨,我不确定你是否得到了这个问题的解决方案。 But here is my answer for this. 但这是我对此的回答。 Using Nunit 3.0 you can achieve this with below lines... 使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM