C# NUnit:访问TearDown()中的失败消息
我正在尝试将NUnit中运行的自动测试结果记录在一个小数据库中,这样数据就可以很容易地访问,并且由于各种原因记录得更安全。(大约有550个自动化测试,运行这些测试可能需要几天时间) 我已经可以访问测试的结束状态(通过/失败/错误/取消/跳过等),但我想记录额外的详细信息 我希望在TearDown()内完成此操作 这是我能找到的最接近的东西,但没有给我一个答案:C# NUnit:访问TearDown()中的失败消息,c#,testing,nunit,automated-tests,teardown,C#,Testing,Nunit,Automated Tests,Teardown,我正在尝试将NUnit中运行的自动测试结果记录在一个小数据库中,这样数据就可以很容易地访问,并且由于各种原因记录得更安全。(大约有550个自动化测试,运行这些测试可能需要几天时间) 我已经可以访问测试的结束状态(通过/失败/错误/取消/跳过等),但我想记录额外的详细信息 我希望在TearDown()内完成此操作 这是我能找到的最接近的东西,但没有给我一个答案: 想法?我相信你能从中获得所需的信息。我自己并没有使用过这些,但我已经把它们添加到书签中,以便做一些类似于您试图完成的事情 这是您要使用
想法?我相信你能从中获得所需的信息。我自己并没有使用过这些,但我已经把它们添加到书签中,以便做一些类似于您试图完成的事情 这是您要使用的界面。希望您的
TearDown
方法将在TestFinished
之前被调用,但我无法验证这一点
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.
//e.g.
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。我正在通过NUnit的GUI(与控制台版本相反)运行selenium测试,测试本身继承一个基本测试类,该类包含安装/拆卸方法以及测试的任何全局值。首先感谢您向我展示这一点(我会+1,但我的代表太低),更深入的版本是不幸的,我不知道如何正确地实现这一点,以便调用我的方法,但他们可以非常简单地更新“errorMessage”属性。好的,感谢您的回答,我发现:。将一个实现此接口的类放在一起,并且仅使用TestStart和TestFinished就足以满足我的需要。这些函数在安装之前和拆卸之后调用,因此您要做的任何额外日志记录都应该在该类中完成。