NUnit 2.6.2 TestContext.CurrentContext始终为null [英] NUnit 2.6.2 TestContext.CurrentContext always null

查看:114
本文介绍了NUnit 2.6.2 TestContext.CurrentContext始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use the TestContext.CurrentContext of NUnit 2.6.2 but it's always null.

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

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. All the properties inside Test and Result are throwing the exception.

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

我可能做错了什么?

推荐答案

根据此

According to this discussion you have to make sure you execute with the correct version of the NUnit testrunner. The version has to be NUnit 2.6.2.

尝试使用具有正确版本的nunit-console运行测试.

Try to run your tests with nunit-console with the correct version.

更新:我确实在VS2012中建立了一个新项目,并使用NuGet添加了NUnit 2.6.2和NUnit.Runners 2.6.2.使用Resharper Testrunner,我没有遇到任何错误,但是也没有控制台输出,因此我确实从<project-folder>\packages\NUnit.Runners.2.6.2\tools\

Update: I did set up a new project in VS2012 and added NUnit 2.6.2 and NUnit.Runners 2.6.2 using NuGet. 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\

这是我收到的输出:

结果看起来还可以. 我在上面运行了您的示例代码.

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

您应该尝试再次使用NUnit.exe运行测试,但是在帮助->关于NUnit ...

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 2.6.2 TestContext.CurrentContext始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆