如何将操作传递到测试用例中 [英] How do I pass an Action into a TestCase
问题描述
我有一个 NUnit 测试
I have an NUnit test
[TestCase(...)]
public void test_name(Action onConfirm)
{
...
}
我想要做的是在 TestCase 属性中将一个 Action 传递到这个测试中,但无论我尝试什么都会失败.我试着把
What I want to do is pass an Action into this test in the TestCase attribute, but no matter what I try keeps failing. I tried to put
() => SomeDummyMethodIDefined()
直接进入测试用例,但没有用.
directly into the TestCase and that didn't work.
我创建了一个动作
Action DummyAction = () => SomeDummyMethodIDefined();
并将 DummyAction 传递到 TestCase 中,但没有奏效.
and pass DummyAction into the TestCase and that didn't work.
有没有办法做到这一点?
Is there a way to do this?
推荐答案
这是一个非常粗略的例子,我的灵感来自于阅读 NUnit 文档在这里
This is a very rough example which I was inspired from reading the NUnit docs here
namespace NUnitLambda
{
using System;
using NUnit.Framework;
[TestFixture]
public class Class1
{
[Test]
[TestCaseSource(typeof(SourceClass), "TestCases")]
public void Foo(Action action)
{
action();
}
}
public class SourceClass
{
private static Action t = () => Console.WriteLine("Hello World");
public static Action[] TestCases = { t };
}
}
尝试一下代码,希望你能从中得到你想要的.作为记录,我使用的是 NUnit 2.6.
Have a play around with the code, hopefully you will get what you want out of it. For the record, I was using NUnit 2.6.
您不必在此处使用 static
例如
You don't have to use static
here either e.g.
public class SourceClass
{
private Action t = () => Console.WriteLine("Hello World");
public Action[] TestCases;
public SourceClass()
{
TestCases = new Action[1];
TestCases[0] = t;
}
}
这篇关于如何将操作传递到测试用例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!