16
votes

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 void 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 );
    }
}
3
Can you build a small, but complete program + plugin and show us the code, that replicates the problem? Or, at the very least, can you post the class file of your plugin that you're trying to test?Lasse V. Karlsen
Could it be that your plugin is compiled with a different NUnit version? I gather that it isn't from what you've written in the question, but still... it would make the attribute types incompatible.Lasse V. Karlsen
if I do Console.WriteLine(loc), it prints the name of the console exe. I tried to load the class dll and I got "Access is denied". Then when I do Console.WriteLine(result.ResultState) it prints "Inconclusive". How do you get the message "has no textfixture"? Also how do you check that it is infact loading your test dll. I might be missing on something obvious here.Kinjal Dixit

3 Answers

14
votes

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 );
    }
}
10
votes

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.

2
votes

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.