tags:

views:

232

answers:

4

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 );
    }
}
A: 

A TestFixtureAttribute is not a TestAttribute.

Normally you decorate the test class with a TestFixture attribute and each of the test methods with a Test attribute. The error indicates that you have not marked your test class with the TestFixture attribute.

[TestFixture]
public class myTestClass
{
  [Test]
  public void aTest()
  {

  }
}
Oded
My class is conforming with your example. In fact, I copied it from nunit documentation. Furthermore, my class contains two methods marked as test, using the Test attribute, just like your example.
Flavio
Are you sure you are loading the _test_ assemblies and not the _application_ assemblies?
Oded
A: 

I think you're king od breaking the point of unit testing. Unit tests are about to test basically small pieces of code (i.e. method by method). According to that, your component classes should be written to be testable.

Since you need to run in application context, I would say the code is rather coupled, since you are not able to provide some mock environment.

For example, there's huge difference between these two implementations (adding just to justify my opinion ;-) )

Hardly testable:

public class Foo
{
    public Foo()
    {
        this.bar = Factory.GetBar();
    }
}

and agile and fully testable:

public class Foo
{
    public Foo(IBar bar)
    {
        this.bar = bar;
    }
}
Pz
A: 

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.

Flavio
A: 

That helped a lot. I find it odd that it seems harder to run a unit test suite from code in NUnit versus JUnit ... there should be better documentation.

mb

Mike Basil