简体   繁体   English

NUnit 3.x-用于后代测试类的TestCaseSource

[英]NUnit 3.x - TestCaseSource for descendant test classes

I currently have a set of unit tests which are consistent for a number of Rest API endpoints. 我目前有一组单元测试,这些单元测试与多个Rest API端点一致。 Say the class is defined like so. 像这样定义类。

public abstract class GetAllRouteTests<TModel, TModule>
{
  [Test]
  public void HasModels_ReturnsPagedModel()
  {
     // Implemented test
  }
}

With the implemented test fixture looking like: 实施的测试夹具看起来像:

[TestFixture(Category = "/api/route-to-test")]
public GetAllTheThings : GetAllRouteTests<TheThing, ModuleTheThings> { }

This enables me to run a number of common tests across all GET all/list routes. 这使我能够跨所有GET all / list路由运行许多通用测试。 It also means that I have classes which are linked directly to the module being tested, and links between tests and code in Resharper / Visual Studio / CI "just work". 这也意味着我拥有直接链接到要测试的模块的类,并且Resharper / Visual Studio / CI中的测试和代码之间的链接“正常工作”。

The challenge is that some routes require query parameters for testing other pathways through the route code; 挑战在于某些路线需要查询参数来测试通过路线代码的其他路径。

eg /api/route-to-test?category=big. 例如/ api / route-to-test?category = big。

As [TestCaseSource] requires a static field, property, or method there appears to be no nice way to override a list of query strings to pass. 由于[TestCaseSource]需要静态字段,属性或方法,因此似乎没有很好的方法来覆盖要传递的查询字符串列表。 The closest thing I have come up with seems like a hack. 我想出的最接近的东西似乎是hack。 Namely: 即:

public abstract class GetAllRouteTests<TModel, TModule>
{
  [TestCaseSource("StaticToDefineLater")]
  public void HasModels_ReturnsPagedModel(dynamic args)
  {
     // Implemented test
  }
}

[TestFixture(Category = "/api/route-to-test")]
public GetAllTheThings : GetAllRouteTests<TheThing, ModuleTheThings> 
{
    static IEnumerable<dynamic> StaticToDefineLater() 
    {
       // yield return all the query things
    }
}

This works because the static method is defined for the implemented test class, and is found by NUnit. 这是可行的,因为为实现的测试类定义了静态方法,并且该方法由NUnit找到。 Huge hack. 巨大的骇客。 Also problematic for someone else consuming the abstract class as they need to "know" to implement "StaticToDefineLater" as a static something. 对于使用抽象类的其他人来说也是个问题,因为他们需要“知道”将“ StaticToDefineLater”实现为静态对象。

I am looking for a better way of achieving this. 我正在寻找一种更好的方法来实现这一目标。 It seems like non-static TestCaseSource sources were removed in NUnit 3.x, so that's out. 似乎非静态TestCaseSource源已在NUnit 3.x中删除,因此可以了。

Thanks in advance. 提前致谢。

NOTES: 笔记:

  • GetAllRouteTests<> implements a number of tests, not just the one shown. GetAllRouteTests <>实现了许多测试,而不仅仅是显示的测试。
  • Iterating through all the routes in one test will "hide" what is covered, so would like to avoid that. 在一个测试中遍历所有路径将“隐藏”所涵盖的内容,因此希望避免这种情况。

The way I solved a similar problem is by having a base source class that implements IEnumerable (another acceptable source for NUnit), consider if this design suits your usecase: 我解决类似问题的方法是拥有一个实现IEnumerable的基类(NUnit的另一个可接受源),请考虑这种设计是否适合您的用例:

// in the parent fixture...
public abstract class TestCases : IEnumerable
{
    protected abstract List<List<object>> Cases { get; }

    public IEnumerator GetEnumerator()
    {
        return Cases.GetEnumerator();
    }
}

// in tests
private class TestCasesForTestFoobar : TestCases
{
    protected override List<List<object>> Cases => /* sets of args */
}

[TestCaseSource(typeof(TestCasesForTestFoobar))]
public void TestFoobar(List<object> args)
{
    // implemented test
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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