基本上我想能够插入到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:无法从“方法组”转换为“对象”
>错误7参数2:无法从“方法组”转换为“对象”
>错误8参数3:无法从“方法组”转换为“对象”
你知道有没有办法得到这样的东西或类似的工作?
解决方法
这是
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 []创建强类型的参数列表.