简体   繁体   English

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

[英]NUnit: Accessing the Failure Message in TearDown()

I'm trying to log the results of automated tests run in NUnit in a small database so that data is easily accessible and more securely logged for various reasons. 我正在尝试将NUnit中运行的自动化测试结果记录在一个小型数据库中,以便数据易于访问并且由于各种原因更安全地记录。 (There's about ~550 automated tests and running them all can take days) (大约有550个自动化测试并且运行它们都需要数天)

I already have access to the ending status of the test (Passed/Failed/Error/Cancelled/Skipped etc..) but I'd like to log the extra detail. 我已经可以访问测试的结束状态(通过/失败/错误/取消/跳过等...)但我想记录额外的细节。

I am looking to do this within TearDown(). 我希望在TearDown()中做到这一点。

This is the closest thing I could find, but did not provide me with an answer: https://groups.google.com/forum/?fromgroups=#!msg/nunit-discuss/lXxwECvpqFc/IbKOfQlbJe8J 这是我能找到的最接近的东西,但没有给我答案: https//groups.google.com/forum/?fromgroups =#! msg / nunit- discuss / lXxwECvpqFc / IbkOfQlbJe8J

Ideas? 想法?

I believe you'll be able to get the information you need with NUnit EventListeners . 我相信您将能够通过NUnit EventListeners获取所需的信息。 I haven't used these myself, but I've had them bookmarked to do something similar to what you're trying to accomplish. 我自己并没有使用过这些,但我已经将它们加入书签,以便做一些类似于你想要完成的事情。

Here's the interface you'd be working with. 这是您正在使用的界面。 Hopefully your TearDown method would be called just before TestFinished , but I can't verify this. 希望您的TearDown方法将在TestFinished之前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);
}

For those that want some skellie code: 对于那些想要一些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 has these details contained inside of TestContext.CurrentContext.~ NUnit 3.0将这些细节包含在TestContext.CurrentContext中.~

Caution: In the event that you have the VS test adapter included as an extension, using the event handler will cause the tests to run twice. 警告:如果您将VS测试适配器作为扩展包括在内,则使用事件处理程序将导致测试运行两次。 Once for the extension and once for the including dll required for implementing the event handler. 一次用于扩展,一次用于实现事件处理程序所需的包含dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM