Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NUnit 3.x-子测试类的TestCaseSource_C#_Unit Testing_Nunit - Fatal编程技术网

C# NUnit 3.x-子测试类的TestCaseSource

C# NUnit 3.x-子测试类的TestCaseSource,c#,unit-testing,nunit,C#,Unit Testing,Nunit,我目前有一组单元测试,它们对于许多RESTAPI端点是一致的。假设类是这样定义的 public abstract class GetAllRouteTests<TModel, TModule> { [Test] public void HasModels_ReturnsPagedModel() { // Implemented test } } 公共抽象类GetAllRouteTests { [测试] public void HasModels_Retur

我目前有一组单元测试,它们对于许多RESTAPI端点是一致的。假设类是这样定义的

public abstract class GetAllRouteTests<TModel, TModule>
{
  [Test]
  public void HasModels_ReturnsPagedModel()
  {
     // Implemented test
  }
}
公共抽象类GetAllRouteTests
{
[测试]
public void HasModels_ReturnsPagedModel()
{
//实施测试
}
}
实现的测试夹具如下所示:

[TestFixture(Category = "/api/route-to-test")]
public GetAllTheThings : GetAllRouteTests<TheThing, ModuleTheThings> { }
[TestFixture(Category=“/api/route to test”)]
public获取所有内容:GetAllRouteTests{}
这使我能够跨所有GET-all/list路由运行许多常见测试。这还意味着我有直接链接到被测试模块的类,以及Resharper/visualstudio/CI“just work”中测试和代码之间的链接

挑战在于,一些路由需要查询参数,以便通过路由代码测试其他路径

e、 g./api/测试路线?类别=大

由于[TestCaseSource]需要一个静态字段、属性或方法,因此似乎没有好办法覆盖要传递的查询字符串列表。我想到的最接近的东西似乎是一个黑客。即:

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
    }
}
公共抽象类GetAllRouteTests
{
[TestCaseSource(“StaticToDefineLater”)]
public void HasModels\u ReturnsPagedModel(动态参数)
{
//实施测试
}
}
[TestFixture(Category=“/api/route to test”)]
公共获取所有内容:获取所有路由测试
{
静态IEnumerable StaticToDefineLater()的
{
//返回所有查询内容
}
}
这是因为静态方法是为实现的测试类定义的,并且是由NUnit找到的。巨大的黑客。对于使用抽象类的其他人来说也是有问题的,因为他们需要“知道”才能将“StaticToDefineLater”实现为一个静态的东西

我正在寻找更好的方法来实现这一点。看起来非静态TestCaseSource源代码在NUnit 3.x中被删除了,所以到此为止

提前谢谢

注:

  • GetAllRouteTests实现了许多测试,而不仅仅是所示的测试
  • 在一个测试中迭代所有路由将“隐藏”所覆盖的内容,因此希望避免这种情况
我解决了类似的问题是通过拥有一个源代码类来实现iNeXDIABLE(另一个可接受的NUnE源),考虑这个设计是否适合您的用例:

// 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
}
//在父设备中。。。
公共抽象类测试用例:IEnumerable
{
受保护的抽象列表案例{get;}
公共IEnumerator GetEnumerator()
{
返回Cases.GetEnumerator();
}
}
//测试中
私有类TestCasesForTestFoobar:TestCases
{
受保护的覆盖列表案例=>/*组参数*/
}
[TestCaseSource(typeof(TestCasesForTestFoobar))]
公共void TestFoobar(列表参数)
{
//实施测试
}

正如我们所希望的那样。:-)此外,将数据放在一个单独的类中比将其放在测试类中要干净得多。哦,整洁-没有比框架的首席开发人员的认可要好得多:)那么是否需要在实现的设备上定义[TestCaseSource*]属性?是否有办法在父设备上定义它,以便在基本/父设备中实现公共测试?@Charlie是否有办法将
TestCaseSource
放在父抽象设备中,而不必在继承它的每个类中发送它?@Vedran该属性显然必须出现在每个测试上。但是数据本身可能在基类中,使用继承装置可以访问的字段、属性或方法。如果在
TestCaseSourceAttribute
构造函数中指定类型,则它也可能位于不相关的类中。