重用具有多个实现的测试套件?

发布于 2024-08-06 15:48:01 字数 158 浏览 3 评论 0原文

我正在使用 nUnit 进行测试。我有一套针对我的 IFoo 界面运行的测试;测试夹具设置确定要加载和测试哪个 IFoo 实现。

我试图弄清楚如何针对 IFoo 实现列表运行相同的套件,但没有看到任何方法可以在不手动修改设置的情况下测试所有实现。

有人解决过这个问题吗?

I'm testing with nUnit. I have a suite of tests that run against my IFoo interface; the Test Fixture Setup determines which IFoo implementation to load and test.

I'm trying to figure out how to run the same suite against a list of IFoo implementaions, but don't see any way to test all implementations without manually modifying the Setup.

Has anyone tackled this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-08-13 15:48:01

创建一个基测试类,其中包含 IFoo 实现之间共享的测试,如下所示:

// note the absence of the TestFixture attribute
public abstract class TestIFooBase
{
   protected IFoo Foo { get; set; }

   [SetUp]
   public abstract void SetUp();

   // all shared tests below    

   [Test]
   public void ItWorks()
   {
      Assert.IsTrue(Foo.ItWorks());
   }
}

现在为要测试的每个实现创建一个非常小的派生类:

[TestFixture]
public class TestBarAsIFoo : TestIFooBase
{
   public override void SetUp()
   {
      this.Foo = new Bar();
   }
}

编辑:显然 NUnit 还支持 参数化测试装置,甚至支持具有参数类型的通用测试装置。链接文档中的示例:

[TestFixture(typeof(ArrayList))]
[TestFixture(typeof(List<int>))]
public class IList_Tests<TList> where TList : IList, new()
{
  private IList list;

  [SetUp]
  public void CreateList()
  {
    this.list = new TList();
  }

  [Test]
  public void CanAddToList()
  {
    list.Add(1); list.Add(2); list.Add(3);
    Assert.AreEqual(3, list.Count);
  }
}

此示例有点简单,因为它具有类型的 new() 约束。但您也可以使用 Activator.CreateInstance 并从 TestFixture 属性传递 IFoo 实现的构造函数参数。

Create a base test class that contains tests shared between IFoo implementations like this:

// note the absence of the TestFixture attribute
public abstract class TestIFooBase
{
   protected IFoo Foo { get; set; }

   [SetUp]
   public abstract void SetUp();

   // all shared tests below    

   [Test]
   public void ItWorks()
   {
      Assert.IsTrue(Foo.ItWorks());
   }
}

Now create a very small derived class for each implementation you want to test:

[TestFixture]
public class TestBarAsIFoo : TestIFooBase
{
   public override void SetUp()
   {
      this.Foo = new Bar();
   }
}

edit: Apparently NUnit also has support for parameterized test fixtures, even generic test fixtures with parameter types are supported. Example from the linked documentation:

[TestFixture(typeof(ArrayList))]
[TestFixture(typeof(List<int>))]
public class IList_Tests<TList> where TList : IList, new()
{
  private IList list;

  [SetUp]
  public void CreateList()
  {
    this.list = new TList();
  }

  [Test]
  public void CanAddToList()
  {
    list.Add(1); list.Add(2); list.Add(3);
    Assert.AreEqual(3, list.Count);
  }
}

This example is a bit simplistic because it has the new() constraint for the types. But you could also use Activator.CreateInstance and pass the constructor arguments for your IFoo implementations from the TestFixture attributes.

情定在深秋 2024-08-13 15:48:01

实现此目的的几种方法之一:

public interface IFoo
{
    string GetName();
}

public class Foo : IFoo
{
    public string GetName()
    {
        return "Foo";
    }
}

public class Bar : IFoo
{
    public string GetName()
    {
        return "Bar";  // will fail
    }
}

public abstract class TestBase
{
    protected abstract IFoo GetFoo();

    [Test]
    public void GetName_Returns_Foo()
    {
        IFoo foo = GetFoo();
        Assert.That(foo.GetName(), Is.EqualTo("Foo"));
    }
}

[TestFixture]
public class FooTests : TestBase
{
    protected override IFoo GetFoo()
    {
        return new Foo();
    }
}

[TestFixture]
public class BarTests : TestBase
{
    protected override IFoo GetFoo()
    {
        return new Bar();
    }
}

One of several ways to accomplish this:

public interface IFoo
{
    string GetName();
}

public class Foo : IFoo
{
    public string GetName()
    {
        return "Foo";
    }
}

public class Bar : IFoo
{
    public string GetName()
    {
        return "Bar";  // will fail
    }
}

public abstract class TestBase
{
    protected abstract IFoo GetFoo();

    [Test]
    public void GetName_Returns_Foo()
    {
        IFoo foo = GetFoo();
        Assert.That(foo.GetName(), Is.EqualTo("Foo"));
    }
}

[TestFixture]
public class FooTests : TestBase
{
    protected override IFoo GetFoo()
    {
        return new Foo();
    }
}

[TestFixture]
public class BarTests : TestBase
{
    protected override IFoo GetFoo()
    {
        return new Bar();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文