【问题标题】:NUnit: Accessing the Failure Message in TearDown()NUnit:在 TearDown() 中访问失败消息
【发布时间】:2013-01-15 15:23:57
【问题描述】:

我正在尝试将在 NUnit 中运行的自动化测试的结果记录在一个小型数据库中,以便出于各种原因轻松访问和更安全地记录数据。 (大约有 550 个自动化测试,运行它们可能需要几天时间)

我已经可以访问测试的结束状态(通过/失败/错误/取消/跳过等),但我想记录额外的详细信息。

我希望在 TearDown() 中执行此操作。

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

想法?

【问题讨论】:

  • 我正在通过 NUnit 的 GUI(与控制台版本相反)运行 selenium 测试,测试本身继承了一个基础测试类,该类包含设置/拆卸方法和任何对测试具有全局价值的东西。

标签: c# testing nunit automated-tests teardown


【解决方案1】:

相信您可以通过NUnit EventListeners 获得所需的信息。我自己没有使用过这些,但我已将它们添加为书签,以执行与您想要完成的任务类似的操作。

这是您要使用的界面。希望您的 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);
}

【讨论】:

  • 首先感谢您向我展示这一点(我想 +1,但我的代表太低了),更深入的版本是 simple-talk.com/dotnet/.net-tools/… 不幸的是,我不知道如何正确实现这一点所以我的方法被调用,但它们可以非常简单地更新“errorMessage”属性。
  • 好的,感谢您的回答,我发现了这个:stackoverflow.com/questions/6664271/nunit-extension。将一个实现此接口的类一起实现并且仅使用 TestStarted 和 TestFinished 就足以满足我的需要。这些是在 SetUp 之前和 TearDown 之后调用的,因此您想要执行的任何额外日志记录也应该在该类中完成。
【解决方案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..

}

【讨论】:

    【解决方案3】:

    NUnit 3.0 将这些详细信息包含在 TestContext.CurrentContext.~

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2012-03-25
      相关资源
      最近更新 更多