9

I am using nunit 2.5.9.10348 and trying to extract the current test name in the TearDown event so I can assign a screengrab filename the test name however it is always null (see the attached image). The private _context variable does have the TestName however this is no use to me!

Has anyone had success using this new TestContext functionality (from 2.5.7).

alt text

redsquare
  • 78,161
  • 20
  • 151
  • 159

3 Answers3

3

From your screenshot I see that _context has keys "TestName" and "Properties". But TestAdapter looks for keys "Test.Name" for Name and "Test.Properties" for Properties. So, there is something wrong with TestContext initialization (I think wrong data was put to Remoting.Messaging.CallContext).

After a little investigation (see comments): NUnit tests should run by NUnit testig environment for Context to be available.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Tried this - no joy. However what I have noticed is that the test name is only null within the debugger. The same tests running inside TeamCity or nunit gui console output the test name. – redsquare Jan 05 '11 at 10:24
  • I didn't know that you run tests without GUI. And this supposed to be the reason - I think CallContext is initialized by testing environment. If you attach debugger to NUnit GUI, you can see CallContext is initialized properly. – Sergey Berezovskiy Jan 05 '11 at 11:27
  • Yes, I did a little investigation - CallContext is initialized during tests execution by NUnit (not by some plugin to Visual Studion). There is NUnit.Core.TestMethod.RunTestInContext() method which sets up context info. So, without running this, context will be empty. – Sergey Berezovskiy Jan 05 '11 at 11:45
0

I had the same issue. It occurred when in a TearDown method I executed method, which actually was to make the teardown

[TearDown]
public void CleanUp()
{
    TestContext.CurrentContext.Test.FullName; //!=null
    someClassInstance.DoTearDown();
}

class SomeClass
{
     public void DoTearDown()
     {
          TestContext.CurrentContext.Test.FullName; //==null
     }
}

I have no idea why, but it seemed so. Is it your case?

UPDATE: Now I looked at the screenshot, so it's not your case :)

dzendras
  • 4,721
  • 1
  • 25
  • 20
0

Same issue with R# test runner. Just downloaded NUnit sources and added a workaround in TestAdapter to make it work with r#

        public string Name
        {
            get
            {
                return (_context["Test.Name"] ?? _context["TestName"]) as string;
            }
        }
Andrea Balducci
  • 2,834
  • 1
  • 21
  • 18