6
votes

I need to run individual C# NUnit tests programmatically. I found another post that was very helpful in showing me how to run an entire set of tests programmatically, but I need to be able to select individual tests.

I thought that setting up a NameFilter would do the trick, but the RemoteTestRunner only seems to think that there's one test in my suite when there are over fifty. Is it really lumping all tests in a single DLL into one gargantuan test? Is there a way that I can separate them out and run individual test cases?

2
What do you mean exactly by run programmatically? Are you talking about starting up another process and passing in command via the command line to run certain tests, or doing it some other way?Zipper
I mean writing code that will execute unit tests by making a function call: CoreExtensions.Host.InitializeService(); TestPackage testPackage = new TestPackage(@"C:\Dev\MyUnitTests.dll"); RemoteTestRunner remoteTestRunner = new RemoteTestRunner(); remoteTestRunner.Load(testPackage); TestResult testResult = remoteTestRunner.Run(new NullListener(), TestFilter.Empty, false, LoggingThreshold.Error);Huitzilopochtli
Basically what I'm trying to do is write code that will run a unit test for me and store the result in some data structure. Instead of running tests from the command line or a GUI, I want to be able to write code that executes NUnit tests. I've got code from the post I linked to that shows me how to programmatically run every test in a given suite, but I want the ability to run selected tests in a suite.Huitzilopochtli
Did you ever figure this out, I am having the same issue using TestName to pass in my individual test but it always comes back as inconclusivepengibot

2 Answers

1
votes

I had to pass a filter as well, just executing

TestResult testResult = remoteTestRunner.Run(new NullListener(), null , false, LoggingThreshold.Error);

ended in a NullReferenceException. I quickly created an empty filter

class EmptyFilter : TestFilter
{

    public override bool Match(ITest test)
    {
        return true;
    }
}

and passed it to the remoteTestRunner.

TestResult testResult = remoteTestRunner.Run(new NullListener(), new EmptyFilter() , false, LoggingThreshold.Error);

That worked. What one could invest a little is to search whether NUnit already has a similar Filter that could be reused rather than creating a custom one.

0
votes

I had to use a SimpleNameFilter and pass to its constructor the name of the unit test I wanted to run. Here's what I have:

SimpleNameFilter filter = new SimpleNameFilter("Google.Maps.Test.Integrations.GeocodingServiceTests.Empty_address");
TestResult testResult = remoteTestRunner.Run(new NullListener(), filter, false, LoggingThreshold.Error);

You can get the fully-qualified test name by either figuring it out from the test itself or by looking at your test's Properties in the NUnit program.