I am writing a test suite for a class library using C# and XUnit. In the library are a number of classes that implement an interface. I could simply copy-paste the same tests for the interface implementation into each test class but I was wondering if there is a neater solution using some test base class which can be inherited from and run automatically without calling each method in turn in each test class.
This article on codeproject demonstrates how it would be done in mstest using an abstract base class marked with TestClass. A summary is given below. However I cannot figure out if there is an equivalent using xunit. Does anyone know how to approach this? The xunit docs say there is no equivalent to TestClass in mstest.
Interface
public interface IDoSomething
{
int SearchString(string stringToSearch, string pattern);
}
One of many classes implementing interface
public class ThisDoesSomething : IDoSomething
{
public int SearchString(string stringToSearch, string pattern);
{
// implementation
}
}
Interface Test base class
[TestClass]
public abstract class IDoSomethingTestBase
{
public abstract IDoSomething GetDoSomethingInstance();
[TestMethod]
public void BasicTest()
{
IDoSomething ids = GetDoSomethingInstance();
Assert.AreEqual("a_string", ids.SearchString("a_string", ".*");
}
}
Test class that tests the class implementing interface
[TestClass]
public class ThisDoesSomething_Tests : IDoSomethingTestBase
{
public override IDoSomething GetDoSomethingInstance()
{
return new ThisDoesSomething();
}
}