Asyranok
Asyranok

Reputation: 985

How to determine if test has passed or failed for use in TearDown

I am trying to find the value that my .Net code uses to alert Visual Studios that a test has failed, passed, or been ignored. I want to be able to use this to determine if a test has passed or failed for basic tracking in our database.

However, short of putting a variable at the end of each test and setting it to "pass", I have no idea how to differentiate between a failed or passed test when running my tear down code.

Thanks for any help!

Upvotes: 1

Views: 3728

Answers (1)

Patrick Quirk
Patrick Quirk

Reputation: 23747

You can use the TestContext:

// Use the necessary namespace
using NUnit.Framework;

...

[TearDown]
public void TearDown()
{
    if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
    {
        // Your test failed, handle it
    }
}

See the documentation here.

Upvotes: 2

Related Questions