C#: How to run NUnit from my code

Posted by Flavio on Stack Overflow See other posts from Stack Overflow or by Flavio
Published on 2010-05-09T17:52:51Z Indexed on 2010/05/09 17:58 UTC
Read the original article Hit count: 301

Filed under:
|

Hello, 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):

        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.

thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about nunit