Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

.net - How to Run NUnit Tests from C# Code

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...