47

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?

TALlama
  • 16,017
  • 8
  • 38
  • 47

4 Answers4

46

(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()
Izzy
  • 1,764
  • 1
  • 17
  • 31
  • 3
    This was originally suggested to use this.GetType().ToString() If you were lazy; however this does not work for TestCaseSource driven tests, you must use one of the two overloads from TestContext to work in all scenarios. – aolszowka Mar 06 '18 at 17:42
  • @aolszowka why did you remove the line instead of adding that information in your edit? – Izzy Mar 19 '18 at 16:50
  • @aolszowka but that's the opposite to what you've done. You've removed information in the post and replaced it in your comment. – Izzy Mar 23 '18 at 09:14
27

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
  • 4,039
  • 3
  • 28
  • 28
  • 1
    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 Sep 02 '10 at 20:09
  • To use it in code, get the TestContext by calling the static CurrentContext method. So, for example: TestContext.CurrentContext.Test.Name – Jan Hettich Sep 22 '11 at 19:28
  • Updated documentation for NUnit 3: https://github.com/nunit/docs/wiki/TestContext – Chris Sears Jun 05 '19 at 17:24
4

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.

Community
  • 1
  • 1
Paddyslacker
  • 1,880
  • 1
  • 14
  • 20
  • This won't handle [TestCase] methods that are called multiple times, but it's close enough to get you the rep. – TALlama Aug 29 '10 at 23:10
1

If we are using TestCaseSource tag then above solutions might not give correct answer

Try using TestContext.CurrentContext.Test.MethodName

Follow the below example

namespace NunitTests
{
public class Class1
{

static List<TestData> Data = new List<TestData>()
        {
            new TestData()
            {
                ...
            }
        };

[Test]
        [TestCaseSource(nameof(TenMBInstance))]
        public void TestCase(TestData value)
        {
            TestContext.CurrentContext.Test.Name; //TestCase(NunitTests..TestData) 
            TestContext.CurrentContext.Test.MethodName; //TestCase
        }
}
}