21

I'm trying to write a simple method that receives a file and runs it using NUnit. The code I managed to build using NUnit's source does not work:

if(openFileDialog1.ShowDialog() != DialogResult.OK)
{
    return;
}

var builder = new TestSuiteBuilder();
var testPackage = new TestPackage(openFileDialog1.FileName);
var directoryName = Path.GetDirectoryName(openFileDialog1.FileName);
testPackage.BasePath = directoryName;
var suite = builder.Build(testPackage);

TestResult result = suite.Run(new NullListener(), TestFilter.Empty);

The problem is that I keep getting an exception thrown by builder.Build stating that the assembly was not found.

What am I missing? Is there some other way to run the test from the code (without using Process.Start)?

1
  • To be honest, I don't know about this way of testing. Guessing blind, have you tried to run this test through the NUnit GUI, or verified whether you had all of the dependencies referenced into your project, or perhaps if the assembly is located where the exception says it can't find it? A test class with NUnit has to have the [TestFixture()] attribute, and the test methods the [Test()] attribute, using NUnit.Framework import. Commented May 16, 2010 at 15:31

1 Answer 1

30

Adding the following line at the beginning, sets up the NUnit framework and might help you:

CoreExtensions.Host.InitializeService();

Another "easier" way to execute NUnit tests programmatically would be:

TestPackage testPackage = new TestPackage(@"C:\YourProject.Tests.dll");
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(testPackage);
TestResult testResult = remoteTestRunner.Run(new NullListener());

You need to reference the following assemblies:

  • nunit.core.dll
  • nunit.core.interfaces.dll

And of course, the nunit.framework.dll must be in the folder with your test assembly ;)

Sign up to request clarification or add additional context in comments.

3 Comments

Is any of this stuff documented? I'd like to implement something like this myself but I'd rather RTFM to see what options are open to me
I found no documentation and had to poke around the source code to get more information. But- while I was there - I found a subtle difference between RemoteTestRunner and SimpleTestRunner - the former calls CoreExtensions.Host.InitializeService for you, while the Simple runner does not (thereby requiring you the user to add it to your code).
I made it run, however, I was not able to get the results to show up anywhere. any suggestions? thanks in advance

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.