same unit test for different implementations

java, mstest

Solution

Use an abstract test class:

[TestClass]
public abstract class SearchTests
{
    private ISearcher _searcherUnderTest;

    [TestSetup]
    public void Setup()
    {
        _searcherUnderTest = CreateSearcher();
    }

    protected abstract ISearcher CreateSearcher();

    [TestMethod]
    public void Test1(){/*do stuff to _searcherUnderTest*/ }

    // more tests...

    [TestClass]
    public class CoolSearcherTests : SearcherTests
    {
         protected override ISearcher CreateSearcher()
         {
             return new CoolSearcher();
         }
    }

    [TestClass]
    public class LameSearcherTests : SearcherTests
    {
         protected override ISearcher CreateSearcher()
         {
             return new LameSearcher();
         }
    }
}

Problem

Let's say I have two implementations of a search algorithm that return the same result for the same input. They both implement the same interface. How can I use a single `[TestClass]` for testing both implementations, rather then create two test files with eventually the same logic ? Can I tell MSUnit to launch one of the tests twice with different constructor parameter? Perhaps I should (n)inject it somehow ?

Original source

Related problems