How to log NUnit Test Error or fail message?

拥有回忆 提交于 2020-02-05 04:56:03

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!