简体   繁体   中英

How do I create a base test class in Xunit to test interface implementations?

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

Works exactly the same way...

public abstract class IDoSomethingTestBase
{
  protected readonly IDoSomething InstanceUnderTest;

  protected IDoSomethingTestBase(IDoSomething instanceUnderTest){
    InstanceUnderTest = instanceUnderTest;
  }

  [Fact]
  public void BasicTest()
  {
    Assert.AreEqual("a_string", InstanceUnderTest.SearchString("a_string", ".*");
  }
}

Actual test class:

public class ThisDoesSomething_Tests : IDoSomethingTestBase
{
  public ThisDoesSomething_Tests(): base(new ThisDoesSomething()) { }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM