开发者

How to run NUnit from my code

I'd like to use NUnit to run unit tests in my plug-in, but it needs to be run in the context of my application. To solve this, I was trying to develop a plug-in that runs NUnit, which in turn will execute my tests in the application's context.

I didn't find a specific documentation on this subject so I dug a piece of information here and there and I came out with the following piece of code (which is similar to one I found here in StackOverflow):

    public static vo开发者_运维知识库id Main()
    {
        SimpleTestRunner runner = new SimpleTestRunner();
        TestPackage package = new TestPackage( "Test" );
        string loc = Assembly.GetExecutingAssembly().Location
        package.Assemblies.Add( loc );
        if( runner.Load(package) )
        {
            TestResult result = runner.Run( new NullListener() );
        }
    }

The result variable says "has no TestFixture" although I know for sure it is there. In fact my test file contains two test.

Using another approach I found, which is summarized by the following code:

TestSuiteBuilder builder = new TestSuiteBuilder();
TestSuite testSuite = builder.Build( package );

// Run tests
TestResult result = testSuite.Run( new NullListener(), NUnit.Core.TestFilter.Empty );

I saw nunit data structures with only 1 test and I had the same error.

For sake of completeness, I am using the latest version of nunit, which is 2.5.5.10112.

Does anyone know what I'm missing? A sample code would be appreciated. My test class is:

[TestFixture]
public class UnitTests
{
    public UnitTests()
    {
    }

    [Test]
    public void TestEqual()
    {
        long a = 10;
        long b = 10;
        Assert.AreEqual( a, b );
    }

    [Test]
    public void TestNotEqual()
    {
        long a = 10;
        long b = 11;
        Assert.AreNotEqual( a, b );
    }
}


The below code help to resolve the issue

public static void Main()
{
    CoreExtensions.Host.InitializeService();
    SimpleTestRunner runner = new SimpleTestRunner();
    TestPackage package = new TestPackage( "Test" );
    string loc = Assembly.GetExecutingAssembly().Location;
    package.Assemblies.Add( loc );
    if( runner.Load(package) )
    {
        TestResult result = runner.Run( new NullListener(),
            TestFilter.Empty, false, LoggingThreshold.Off );
    }
}


I've posted my question in the NUnit forum and Charlie gave me a tip on how to find the problem. I think it might be a good idea to post it here so others could prevent to spend a lot of time on it. The solution was to initialize the core services first with the following line:

CoreExtensions.Host.InitializeService();

thanks all.


I had a lot of trouble with the SimpleTestRunner() class as well. At the end I switched to the RemoteTestRunner() class for the execution of unit tests programmatically. The implementation is much easier and best of all it works.

TestPackage package = new TestPackage(@"C:\YourProject.Tests.dll");
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(package);
TestResult result = 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜