问题
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:
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.
来源:https://stackoverflow.com/questions/11830556/how-to-log-nunit-test-error-or-fail-message