在单个测试类中测试接口的多个实现

Ufu*_*arı 5 .net c# unit-testing xunit.net

我需要通过上一流水平的测试数据,但是TheoryInlineData属性只能在方法中使用.

public class ContainerTests : TestFixture
{
    private IContainer _container;

    public ContainerTests(string containerName)
    {
        _container = CreateContainer(containerName);
    }

    [Fact]
    public void ResolveJobFactory()
    {
        IJobFactory jobFactory = _container.Resolve<IJobFactory>();
    }

    private IContainer CreateContainer(string containerName)
    {
        if (containerName == "CastleWindsor")
        {
            return new WindsorContainerAdapter();
        }
        //other adapters

        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在xUnit.net中实现类似的东西?

Chr*_*ris 4

在单个测试类中测试接口的多个实现

不!每个实现都有一个测试类。

编辑:根据评论,我建议有一个包含所有常见测试的基 TestClass,然后每个实现都有一个从该基类继承的测试类。

  • 如果是前者,那么您可以编写一个基测试类,其中包含针对 IJobFactory 实现的所有测试。然后,您将为 IJobFactory 的每个实现编写一个从此基继承的测试类。每个继承者只负责构建 IJobFactory 的实现,然后基类将完成其余的工作。 (2认同)