redsquare
redsquare

Reputation: 78667

Nunit - TestContext.CurrentContext.Test not working

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

Upvotes: 9

Views: 11913

Answers (3)

Andrea Balducci
Andrea Balducci

Reputation: 2852

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

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

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.

Upvotes: 3

dzendras
dzendras

Reputation: 4751

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 :)

Upvotes: 0

Related Questions