【问题标题】:NUnit 2.6.2 TestContext.CurrentContext always nullNUnit 2.6.2 TestContext.CurrentContext 始终为空
【发布时间】:2013-08-30 08:34:03
【问题描述】:

我正在尝试使用 NUnit 2.6.2 的 TestContext.CurrentContext,但它始终为空。

我想要输出带有测试结果的输出,但如果我运行以下代码,我总是会在 TearDown 方法中得到一个 NullReferenceException。 Test 和 Result 中的所有属性都在抛出异常。

[TestFixture]
public class UtilitiesTests
{
  [TearDown]
  public void TearDown()
  {
    //using console here just for sake of simplicity. 
    Console.WriteLine(String.Format("{0}: {1}", TestContext.CurrentContext.Test.FullName, TestContext.CurrentContext.Result.Status));
  }

  [Test]
  public void CleanFileName()
  {
    var cleanName = Utilities.CleanFileName("my &file%123$99\\|/?\"*:<>.jpg");
    Assert.AreEqual("my-efile12399---.jpg", cleanName);
  }
}

我可能做错了什么?

【问题讨论】:

  • 你是如何运行测试的?
  • 我正在使用 coderush。我也尝试过使用 Nunit Gui。相同的行为
  • 我猜CurrentContext.TestSetupTearDown 中不可用。您确定 CurrentContext 为 null 且不是其属性之一吗?
  • 上下文不为空,而属性为空。我发现了一些在拆解中使用它的示例,但使用的是 nunit 2.5。也许在 2.6 中不再工作了

标签: c# tdd nunit nunit-2.6.2


【解决方案1】:

根据discussion,您必须确保使用正确版本的 NUnit 测试运行程序执行。版本必须是 NUnit 2.6.2。

尝试使用正确版本的 nunit-console 运行您的测试。

更新:我确实在 VS2012 中建立了一个新项目,并使用 NuGet 添加了 NUnit 2.6.2 和 NUnit.Runners 2.6.2。使用 Resharper Testrunner 我没有收到任何错误,但也没有控制台输出,所以我确实从 &lt;project-folder&gt;\packages\NUnit.Runners.2.6.2\tools\ 运行了 NUnit.exe

这是我收到的输出:

结果看起来不错。 我在上面运行了您的示例代码。

但是,我必须修改您的代码才能运行它:

using System;
using NUnit.Framework;

[TestFixture]
public class UtilitiesTests
{
    [TearDown]
    public void TearDown()
    {
        //using console here just for sake of simplicity. 
        Console.WriteLine(String.Format("{0}: {1}", TestContext.CurrentContext.Test.FullName, TestContext.CurrentContext.Result.Status));
    }

    [Test]
    public void CleanFileName()
    {
        var cleanName = "my &file%123$99\\|/?\"*:<>.jpg";
        Assert.AreEqual("my &file%123$99\\|/?\"*:<>.jpg", cleanName);
    }
}

您应该再次尝试使用 NUnit.exe 运行您的测试,但在验证您在帮助 -> 关于 NUnit ... 中验证您的版本是否正确之前...

我的看起来像这样:

【讨论】:

  • 正如我在 cmets 中所写,我也尝试使用 2.6.2 的 NUnit GUI,但没有运气。
  • 请看我上面的评论。
  • 我能够解决这个问题。我确定 2.6.2,但 dll 来自 nuget,来自 2.6.2,而 gui 是 2.6.1。所以我删除并重新安装了一切,现在它可以工作了。非常感谢我错过了一件简单的事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 2019-07-18
  • 2019-08-18
  • 2011-12-29
  • 2019-07-02
  • 2019-05-04
  • 1970-01-01
相关资源
最近更新 更多