2
votes

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.

1
have you googled for logging or tracking. - JSJ
I can log lots of information during the test, but I want to avoid adding logging code to every single place the test might fail, and just retrieve the failure message from NUnit - breed052

1 Answers

3
votes

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.