tags:

views:

60

answers:

2

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?

+2  A: 

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.

Paddyslacker
This won't handle [TestCase] methods that are called multiple times, but it's close enough to get you the rep.
TALlama
+2  A: 

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

Martin R-L
This works great. I use a Regex to strip out any characters that don't work in a directory name, and it's all good. Thanks!
TALlama