tags:

views:

262

answers:

2

I've current got a line at the top of all my tests that looks like this:

Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

Seems like it'd be nice if I could just put this in my Init method (the one tagged with [Setup]). I thought this would work but no matter what number I put in for the stack frame the closest I can get is Init. Not what I want, but very close:

string methodName = new StackFrame(0).GetMethod().Name;
Console.WriteLine(methodName);

I think this just might not be possible, given the way that Nunit runs tests.

Why do this, you say? Because in my console output it'd be nice to see where a new test started so if they both hit the same code and output different values, I'll know which one did what, without having to debug.

A: 

You can use NUnit.Core.NUnitFramework.TestCaseAttribute to get the current test name.

Frank Schwieterman
well the first problem is that that doesn't exist. Closest I found was `NUnit.Core.NUnitFramework.TestAttribute` which returned a value of `"NUnit.Framework.TestAttribute"`.
jcollum
+2  A: 

TestCaseAttribute wasn't added until NUnit 2.5, which could be why you can't find it. But it wouldn't help anyway -- unfortunately, the SetUp method is called before the test method, not from within the context of the test method (so it won't show up in the call stack).

Fortunately, NUnit already supports what you're trying to do! From the GUI, go to Tools > Options > GUI > General > Text Output and check 'Display TestCase Labels'.

Jo