views:

36

answers:

2

I'm testing with nUnit. I have a suite of tests that run against my IFoo interface; the Test Fixture Setup determines which IFoo implementation to load and test.

I'm trying to figure out how to run the same suite against a list of IFoo implementaions, but don't see any way to test all implementations without manually modifying the Setup.

Has anyone tackled this problem?

+4  A: 

Create a base test class that contains tests shared between IFoo implementations like this:

// note the absence of the TestFixture attribute
public abstract class TestIFooBase
{
   protected IFoo Foo { get; set; }

   [SetUp]
   public abstract void SetUp();

   // all shared tests below    

   [Test]
   public void ItWorks()
   {
      Assert.IsTrue(Foo.ItWorks());
   }
}

Now create a very small derived class for each implementation you want to test:

[TestFixture]
public class TestBarAsIFoo : TestIFooBase
{
   public override void SetUp()
   {
      this.Foo = new Bar();
   }
}
Wim Coenen
+1 for getting there first. ;)
TrueWill
+1  A: 

One of several ways to accomplish this:

public interface IFoo
{
    string GetName();
}

public class Foo : IFoo
{
    public string GetName()
    {
        return "Foo";
    }
}

public class Bar : IFoo
{
    public string GetName()
    {
        return "Bar";  // will fail
    }
}

public abstract class TestBase
{
    protected abstract IFoo GetFoo();

    [Test]
    public void GetName_Returns_Foo()
    {
        IFoo foo = GetFoo();
        Assert.That(foo.GetName(), Is.EqualTo("Foo"));
    }
}

[TestFixture]
public class FooTests : TestBase
{
    protected override IFoo GetFoo()
    {
        return new Foo();
    }
}

[TestFixture]
public class BarTests : TestBase
{
    protected override IFoo GetFoo()
    {
        return new Bar();
    }
}
TrueWill