Vitaliy
Vitaliy

Reputation: 8206

How can I determine the name of my unit test before its execution?

I was using MSTest and all was fine. Not long ago, I needed to write a large number of data driven unit tests.

Moreover, I needed to know the name of the test just before I run it, so I could populate the data sources with the correct parameters (that were fetched from an external remote service).

Nowhere in MSTest could I find a way to get the name of the tests that are about to run before their actual execution. At this point it was, of course, already too late, since the data sources were already populated.

What I need is to know the names of the test that are about to execute so I could configure their data sources in advance, before their execution.

Somebody suggested I "check out NUnit". I am completely clueless about NUnit. For now I have started reading its documentation but am still at a loss. Have you any advice?

Upvotes: 0

Views: 393

Answers (3)

bryanbcook
bryanbcook

Reputation: 18078

If you really need the test's name -- It's not well documented, but NUnit exposes a feature that let's you get access to the current test information:

namespace NUnitOutput.Example
{
  using NUnit.Framework;

  [TestFixture]
  public class Demo
  {
      [Test]
      public void WhatsMyName()
      {
          Console.WriteLine(TestContext.CurrentContext.Test.FullName);
          Console.WriteLine(TestContext.CurrentContext.Test.Name);
      }
  }
}

Provides:

NUnitOutput.Example.Demo.WhatsMyName
WhatsMyName

Note this feature isn't guaranteed to implemented by custom TestRunners, like ReSharper. I have tested this in NUnit 2.5.9 (nunit.exe and nunit-console.exe)

However, re-reading your question I think you should check out is the TestCaseSource or TestCase attribute that can be used to parameterize your tests.

Upvotes: 1

murrekatt
murrekatt

Reputation: 6129

For things like this you should rely on a fixture to initialize the state you want before you run the test.

The simplest way which works in (any) testing framework is to create a fixture which loads any data given a data identifier (string). Then in each test case you just provide the test string for data lookup for the data you want in that test.

Aside from this, it's not recommended to have unit tests access files and other external resources because it means slower unit tests and higher probability of failure (as you're relying on something outside the in-memory code). This of course depends on the amount of data you have and the type of testing you're doing, but I generally have the data compiled-in for unit tests.

Upvotes: 0

razlebe
razlebe

Reputation: 7144

If I'm understanding your problem correctly, you want to get the name of the currently-running test so that you can use it as a key to look up a set of data with which to populate the data sources used by the code under test. Is that right?

If that's the case then I don't think you need to look for special functionality in your unit testing framework. Why not use the Reflection API to fetch the name of the currently-executing method? System.Reflection.MethodBase.GetCurrentMethod() will get you a MethodBase object representing the method, and that has a Name property.

However, I'd suggest that using the method name as a key for looking up the appropriate test data is a bad idea. You'd be coupling the name of the method to your data set, and that seems like a recipe for fragile code to me. You need to be remain free to refactor your code and the names of methods without worrying about whether that will break the database lookup behind-the-scenes.

As an alternative, why not consider creating a custom Attribute that you can use to mark those test methods that need a database lookup, and using a property on that attribute to hold the key?

Upvotes: 0

Related Questions