如何在 Xunit 中创建基础测试 class 来测试接口实现?
How do I create a base test class in Xunit to test interface implementations?
我正在使用 C# 和 XUnit 为 class 库编写测试套件。库中有许多实现接口的 classes。我可以简单地将接口实现的相同测试复制粘贴到每个测试 class 中,但我想知道是否有使用一些测试基础 class 的更简洁的解决方案,它可以继承自和 运行 无需在每次测试中依次调用每个方法class。
此 article on codeproject 演示了如何使用标有 TestClass 的抽象基础 class 在 mstest 中完成。总结如下。但是我不知道是否有使用 xunit 的等价物。有谁知道如何处理这个? xunit 文档说在 mstest.
中没有等同于 TestClass 的东西
界面
public interface IDoSomething
{
int SearchString(string stringToSearch, string pattern);
}
实现接口class的众多
之一
public class ThisDoesSomething : IDoSomething
{
public int SearchString(string stringToSearch, string pattern);
{
// implementation
}
}
接口测试基地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", ".*");
}
}
测试 class 测试 class 实现接口
[TestClass]
public class ThisDoesSomething_Tests : IDoSomethingTestBase
{
public override IDoSomething GetDoSomethingInstance()
{
return new ThisDoesSomething();
}
}
工作方式完全相同...
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", ".*");
}
}
实测class:
public class ThisDoesSomething_Tests : IDoSomethingTestBase
{
public ThisDoesSomething_Tests(): base(new ThisDoesSomething()) { }
}
我正在使用 C# 和 XUnit 为 class 库编写测试套件。库中有许多实现接口的 classes。我可以简单地将接口实现的相同测试复制粘贴到每个测试 class 中,但我想知道是否有使用一些测试基础 class 的更简洁的解决方案,它可以继承自和 运行 无需在每次测试中依次调用每个方法class。
此 article on codeproject 演示了如何使用标有 TestClass 的抽象基础 class 在 mstest 中完成。总结如下。但是我不知道是否有使用 xunit 的等价物。有谁知道如何处理这个? xunit 文档说在 mstest.
中没有等同于 TestClass 的东西界面
public interface IDoSomething
{
int SearchString(string stringToSearch, string pattern);
}
实现接口class的众多
之一public class ThisDoesSomething : IDoSomething
{
public int SearchString(string stringToSearch, string pattern);
{
// implementation
}
}
接口测试基地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", ".*");
}
}
测试 class 测试 class 实现接口
[TestClass]
public class ThisDoesSomething_Tests : IDoSomethingTestBase
{
public override IDoSomething GetDoSomethingInstance()
{
return new ThisDoesSomething();
}
}
工作方式完全相同...
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", ".*");
}
}
实测class:
public class ThisDoesSomething_Tests : IDoSomethingTestBase
{
public ThisDoesSomething_Tests(): base(new ThisDoesSomething()) { }
}