简体   繁体   English

NUnit 2.6.2 TestContext.CurrentContext始终为null

[英]NUnit 2.6.2 TestContext.CurrentContext always null

I'm trying to use the TestContext.CurrentContext of NUnit 2.6.2 but it's always null. 我正在尝试使用NUnit 2.6.2的TestContext.CurrentContext ,但它始终为null。

What I would like is to have an output with the result of tests, but if I run the following code I always get a NullReferenceException in the TearDown method. 我想要的是测试结果的输出,但是如果我运行以下代码,则在TearDown方法中始终会得到NullReferenceException All the properties inside Test and Result are throwing the exception. 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);
  }
}

What I'm possibly doing wrong? 我可能做错了什么?

According to this discussion you have to make sure you execute with the correct version of the NUnit testrunner. 根据讨论,您必须确保使用正确版本的NUnit testrunner执行。 The version has to be NUnit 2.6.2. 版本必须是NUnit 2.6.2。

Try to run your tests with nunit-console with the correct version. 尝试使用具有正确版本的nunit-console运行测试。

Update: I did set up a new project in VS2012 and added NUnit 2.6.2 and NUnit.Runners 2.6.2 using NuGet. 更新:我确实在VS2012中建立了一个新项目,并使用NuGet添加了NUnit 2.6.2和NUnit.Runners 2.6.2。 With the Resharper Testrunner I did get no error but also no Console output, so I did run NUnit.exe from <project-folder>\\packages\\NUnit.Runners.2.6.2\\tools\\ 使用Resharper Testrunner,我没有出错,也没有控制台输出,因此我确实从<project-folder>\\packages\\NUnit.Runners.2.6.2\\tools\\运行NUnit.exe <project-folder>\\packages\\NUnit.Runners.2.6.2\\tools\\

This is the output I recieved: 这是我收到的输出:

在此处输入图片说明

The result looks ok. 结果看起来还可以。 I ran your example code above. 我在上面运行了您的示例代码。

However, I had to modify your code so I could run it: 但是,我必须修改您的代码才能运行它:

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);
    }
}

You should try to run your tests using NUnit.exe again, but before verify that you have the correct verison in Help -> About NUnit ... 您应该尝试再次使用NUnit.exe运行测试,但是在“帮助->关于NUnit ...

Mine looks like this: 我的看起来像这样:

在此处输入图片说明

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

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