NUnit:在TearDown中访问失败消息()

我正在尝试将NUnit中运行的自动化测试的结果记录在一个小型数据库中,以便数据易于访问并且由于各种原因更安全地记录。 (大约有550个自动化测试并运行它们都需要数天)

我已经可以访问测试的结束状态(Passed / Failed / Error / Cancelled / Skipped等..)但我想记录额外的细节。

我希望在TearDown()中做到这一点。

这是我能找到的最接近的东西,但没有给我答案: https : //groups.google.com/forum/?fromgroups =#! msg / nunit- discuss / lXxwECvpqFc / IbKOfQlbJe8J

想法?

我相信您将能够通过NUnit EventListeners获取所需的信息。 我自己并没有使用过它们,但是我已经将它们加入书签以做类似于你想要完成的事情。

这是您正在使用的界面。 希望您的TearDown方法将在TestFinished之前TestFinished ,但我无法validation这一点。

 public interface EventListener { void RunStarted(string name, int testCount ); void RunFinished(TestResult result); void RunFinished(Exception exception); void TestStarted(TestName testName); void TestFinished(TestResult result); void SuiteStarted(TestName testName); void SuiteFinished(TestResult result); void UnhandledException(Exception exception); void TestOutput(TestOutput testOutput); } 

对于那些想要一些skellie代码的人:

 [NUnitAddinAttribute(Type = ExtensionType.Core, Name = "Database Addin", Description = "Writes test results to the database")] public class MyExtension :IAddin, EventListener { //some private attributes to hold important data //you must provide the Install method public bool Install(IExtensionHost host) { //I also built my connection string in here IExtensionPoint listeners = host.GetExtensionPoint("EventListeners"); if (listeners == null) return false; listeners.Install(this); return true; } //you must also provide all the event handlers, //but they don't have to actually do anything if they are not used. //eg public void TestStarted(NUnit.Core.TestName testName) { //This saved the start time of the test _start = DateTime.Now; } public void TestFinished(NUnit.Core.TestResult result) { //LogTest connected to the databse and executed a proc to //insert the log, was quite simple LogTest((result.Message == null? "" : result.Message), result.ResultState, result.Name, _start, DateTime.Now); } public void TestOutput(NUnit.Core.TestOutput testOutput) { //this is one of the unused event handlers, it remains empty. } //etc.. } 

NUnit 3.0包含TestContext.CurrentContext中的这些细节.~

警告:如果您将VS测试适配器作为扩展包括在内,则使用事件处理程序将导致测试运行两次。 一次用于扩展,一次用于实现事件处理程序所需的包含dll。