Reusing test suites with multiple implementations?

I am testing nUnit. I have a set of tests that work against my IFoo interface; Test Fixture Setup determines which IFoo implementation to load and test.

I am trying to figure out how to run the same package with a list of IFoo implementations, but I see no way to test all implementations without manually modifying the installer.

Has anyone solved this problem?

+5
source share
2 answers

Create a base test class that contains tests common to IFoo implementations as follows:

// 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 that you want to test:

[TestFixture]
public class TestBarAsIFoo : TestIFooBase
{
   public override void SetUp()
   {
      this.Foo = new Bar();
   }
}

edit. -, NUnit , , :

[TestFixture(typeof(ArrayList))]
[TestFixture(typeof(List<int>))]
public class IList_Tests<TList> where TList : IList, new()
{
  private IList list;

  [SetUp]
  public void CreateList()
  {
    this.list = new TList();
  }

  [Test]
  public void CanAddToList()
  {
    list.Add(1); list.Add(2); list.Add(3);
    Assert.AreEqual(3, list.Count);
  }
}

, new() . Activator.CreateInstance IFoo TestFixture.

+11

:

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();
    }
}
+1

All Articles