简体   繁体   中英

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 . 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.

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. 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...

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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