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?

4 Answers 4

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()
3
  • 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 6, 2018 at 17:42
  • @aolszowka why did you remove the line instead of adding that information in your edit?
    – Izzy
    Mar 19, 2018 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, 2018 at 9: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

3
  • 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 2, 2010 at 20:09
  • To use it in code, get the TestContext by calling the static CurrentContext method. So, for example: TestContext.CurrentContext.Test.Name Sep 22, 2011 at 19:28
  • Updated documentation for NUnit 3: github.com/nunit/docs/wiki/TestContext Jun 5, 2019 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.

1
  • This won't handle [TestCase] methods that are called multiple times, but it's close enough to get you the rep.
    – TALlama
    Aug 29, 2010 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
        }
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.