有没有办法将代理传递给NUnit TestCase或TestFixture?

Bry*_*Mau 14 c# delegates nunit parameterized-unit-test

基本上我希望能够将方法插入到NUnit中的TestCase或TestFixture来改变行为.本质上我想这样做:

[TestFixture]
public class MethodTests
{
    public delegate void SimpleDelegate();

    public static void A()
    {
        // Do something meaningful
    }

    public static void B()
    {
        // Do something meaningful
    }

    public static void C()
    {
        // Do something meaningful
    }

    [TestCase(A,B,C)]
    [TestCase(C,A,B)]
    [TestCase(C,B,A)]
    public void Test(SimpleDelegate action1, SimpleDelegate action2, SimpleDelegate action3 )
    {
        action1();
        action2();
        action3();
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到的[TestCase(A,B,C)]的错误是

  • 错误6参数1:无法从'方法组'转换为'对象'
  • 错误7参数2:无法从"方法组"转换为"对象"
  • 错误8参数3:无法从'方法组'转换为'对象'

你知道是否有办法让这个或类似的东西工作?

And*_*son 17

这是TestCaseSourceAttribute拯救的地方.

首先,定义一个包含测试用例列表的对象数组.接着,通过参考到对象阵列作为调用测试用例Test[TestCaseSource].这应该按照您的意图构建和运行.

private static readonly object[] TestCases =
{
    new SimpleDelegate[] { A, B, C },
    new SimpleDelegate[] { C, A, B },
    new SimpleDelegate[] { C, B, A }
};

[Test, TestCaseSource("TestCases")]
public void Test(SimpleDelegate action1, SimpleDelegate action2, 
                 SimpleDelegate action3)
{
    action1();
    action2();
    action3();
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更复杂的参数列表,则可以使用例如Tuple而不是SimpleDelegate[]创建强类型参数列表.