Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the NUnit test name programmatically?

Tags:

naming

nunit

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?

like image 327
TALlama Avatar asked Aug 27 '10 22:08

TALlama


People also ask

How do you name a NUnit test?

Naming your tests The name of your test should consist of three parts: The name of the method being tested. The scenario under which it's being tested. The expected behavior when the scenario is invoked.

What is test attribute in NUnit?

The Test attribute is one way of marking a method inside a TestFixture class as a test. It is normally used for simple (non-parameterized) tests but may also be applied to parameterized tests without causing any extra test cases to be generated.

How do I order test cases in NUnit?

There is no facility in NUnit to order tests globally. Tests with an OrderAttribute argument are started before any tests without the attribute. Ordered tests are started in ascending order of the order argument. Among tests with the same order value or without the attribute, execution order is indeterminate.

What is test runner in NUnit?

The nunit.exe program is a graphical runner. It shows the tests in an explorer-like browser window and provides a visual indication of the success or failure of the tests. It allows you to selectively run single tests or suites and reloads automatically as you modify and re-compile your code.


1 Answers

(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() 
like image 125
Izzy Avatar answered Sep 20 '22 13:09

Izzy