1

I'm trying to use the TestContext.CurrentContext of NUnit 2.6.2 but it's always 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. 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);
  }
}

What I'm possibly doing wrong?

Iridio
  • 9,213
  • 4
  • 49
  • 71
  • How are you running the tests? – Arran Aug 30 '13 at 08:46
  • I'm using coderush.I also tried with the Nunit Gui. Same behaviour – Iridio Aug 30 '13 at 09:06
  • *I guess* that `CurrentContext.Test` is not available in `Setup` and `TearDown`. Are you sure that `CurrentContext` is null and not one of its properties? – Dio F Sep 02 '13 at 08:10
  • The context is not null, while the properties are. I found some samples that use it in teardown, but with nunit 2.5. maybe in 2.6 is not working anymore – Iridio Sep 03 '13 at 16:21

1 Answers1

1

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.

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

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\

This is the output I recieved:

enter image description here

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 ...

Mine looks like this:

enter image description here

Jesko R.
  • 827
  • 10
  • 21
  • As I wrote in the comments, I tried also with the NUnit GUI of the 2.6.2, but no luck. – Iridio Sep 09 '13 at 06:33
  • Please see my comment above. – Jesko R. Sep 09 '13 at 09:42
  • I was able to solve the problem. I was sure about the 2.6.2, but the dll, from nuget where from 2.6.2, while the gui was 2.6.1. So I removed and reinstalled everithing and now it works. Thanks a lot I missed a simple thing – Iridio Sep 09 '13 at 15:01