-->

NUnit: Accessing the Failure Message in TearDown()

2019-07-18 06:14发布

我试图登录的NUnit的在一个小型的数据库进行自动测试的结果,这样的数据是方便和更安全地记录各种原因。 (有大约〜550自动化测试和运行它们都可能需要数天)

我已经有机会获得测试(合格/不合格/错误/取消/跳过等)的结束状态,但我想记录的额外细节。

我期待中的TearDown中做到这一点()。

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

想法?

Answer 1:

我相信你就可以得到你所需要的信息NUnit的事件侦听器 。 我还没有使用这些我自己,但我让他们用书签做类似你要完成的事情。

这里是你要共事的接口。 希望您的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);
}


Answer 2:

对于那些想要一些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..

}


Answer 3:

NUnit的3.0包含了TestContext.CurrentContext内这些细节。〜

注意:在你有包括作为扩展的VS测试适配器,使用事件处理程序会导致测试运行两次事件。 一旦延长,一次用于执行事件处理程序所需的DLL,包括。



文章来源: NUnit: Accessing the Failure Message in TearDown()