c# – 有没有办法将代理传递给NUnit TestCase或TestFixture?
发布时间:2020-12-15 04:11:09 所属栏目:百科 来源:网络整理
导读:基本上我想能够插入到NUnit中的TestCase或TestFixture方法来改变行为.实质上我想这样做: [TestFixture]public class MethodTests{ public delegate void SimpleDelegate(); public static void A() { // Do something meaningful } public static void B()
基本上我想能够插入到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,A)] public void Test(SimpleDelegate action1,SimpleDelegate action2,SimpleDelegate action3 ) { action1(); action2(); action3(); } } [TestCase(A,C)]的错误是 >错误6参数1:无法从“方法组”转换为“对象” 你知道有没有办法得到这样的东西或类似的工作? 解决方法
这是
TestCaseSourceAttribute的救援.
首先,定义一个包含测试用例列表的对象数组.接下来,通过引用对象数组作为Test的[TestCaseSource]来调用测试用例.这应该根据您的意图建立和运行. private static readonly object[] TestCases = { new SimpleDelegate[] { A,C },new SimpleDelegate[] { C,B },A } }; [Test,TestCaseSource("TestCases")] public void Test(SimpleDelegate action1,SimpleDelegate action3) { action1(); action2(); action3(); } 如果你需要一个更复杂的参数列表,你可以使用Tuple而不是SimpleDelegate []创建强类型的参数列表. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |