How to access the NUnit test name programmatically?

Is there some global state somewhere that I can access the currently-running test name?

I have tests which output files into a directory and read them back in. I'd like each test to create a directory to play in and then clean up after itself, and I don't want to push that name in (I'd have to make it unique, and then make sure each test keeps it unique; ew). I could use a GUID, but I'd like helper methods to be able to assume "this is the place where test files should be stored" without having to push that GUID around to them. Again, this augers for a global state somewhere.

Basically, I want a call like TestRunner.Current.CurrentTest.Name. Does such a thing exist?


(Assuming c#)

NUnit.Framework.TestContext.CurrentContext.Test.Name 

or

NUnit.Framework.TestContext.CurrentContext.Test.FullName

or if you are really lazy and aren't driving your tests with TestCaseSource (thanks @aolszowka):

this.GetType().ToString()

I haven't upgraded to 2.5.7 yet myself, but it includes a TestContext class that seems to provide just what you're looking for: http://www.nunit.org/index.php?p=releaseNotes&r=2.5.7


Assuming one method per Test, in your NUnit code, you can use reflection to get the method name from the stacktrace.

If you write a helper method in your NUnit code called by other methods to do this file logging, you can use this syntax to check for the previous method:

string MethodName = new StackFrame(1).GetMethod().Name;

See the answers to question 44153, "Can you use reflection to find the name of the currently executing method?" for more details.