breed052
breed052

Reputation: 195

How to log NUnit Test Error or fail message?

I have a set of NUnit tests running and I would like to log the results of the test (along with some environment information) to a DB in the Teardown method after each test completes. Is there any way to get that information from the NUnit TestContext apart from writing my own NUnit add-in? I know that the fail or error messages get logged to whatever output file I specify with the console runner, but I would really like to do it programmatically.

Upvotes: 2

Views: 5060

Answers (1)

ragamufin
ragamufin

Reputation: 4162

You have access to the TestContext variable in your code and can use it to get various information about your test for instance:

[TearDown]
public void TearDown()
{
    if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
    {
        Console.WriteLine(TestContext.CurrentContext.Test.FullName);
        Console.WriteLine(TestContext.CurrentContext.Result.Status);
    }
}

In your TearDown method then you could simply write that data to a db along with whatever else you want.

Upvotes: 3

Related Questions