How do I pass an Action into a TestCase

故事扮演 提交于 2019-12-10 00:16:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!