问题
I have an NUnit test
[TestCase(...)]
public void test_name(Action onConfirm)
{
...
}
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.
I created an Action
Action DummyAction = () => SomeDummyMethodIDefined();
and pass DummyAction into the TestCase and that didn't work.
Is there a way to do this?
回答1:
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 };
}
}
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.
EDIT:
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;
}
}
来源:https://stackoverflow.com/questions/20682219/how-do-i-pass-an-action-into-a-testcase