C#NUnit的TestCaseSource传递参数 [英] C# NUnit TestCaseSource Passing Parameter

查看:484
本文介绍了C#NUnit的TestCaseSource传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的方法,生成一组测试案例

 公开的IEnumerable< ResultsOfCallMyMethod> PrepareTestCases(参数1)
{
的foreach(在输入的字符串项)
{
收益率的回报callMyMethod(参数1);
}
}



如何传递参数是字符串类型的参数传递给我的 PrepareTestCases()



有没有办法做到以下几点:

  [测试,类别(集成),TestCaseSource(PrepareTestCases,参数1)] 
公共无效TestRun(ResultsOfCallMyMethod TESTDATA)
{
//做点什么!
}


解决方案

如果你看一下<一个HREF =http://nunit.org/index.php?p=testCaseSource&r=2.6.4> TestCaseSourceAttribute DOC 你会看到有没有什么办法可以将参数传递给它返回的测试方法案例。



的方法,其生成测试的情况下,应



因此,假设你要避免重复代码,你需要重复使用相同的方法来生成一些测试案例名单,我建议你做以下内容:




  1. 写这实际上产生的测试用例集的参数化方法:结果
    PrepareTestCases()已经这样做了)

     公开的IEnumerable< ResultsOfCallMyMethod> PrepareTestCases(字符串参数)
    {
    的foreach(在输入的字符串项)
    {
    收益率的回报CallMyMethod(参数);
    }
    }


  2. 写参数的包装它称之为测试用例生成而且通过所需的参数:

     公开的IEnumerable< ResultsOfCallMyMethod> PrepareTestCases_Param1()
    {
    返回PrepareTestCases(参数1);
    }

    公开的IEnumerable< ResultsOfCallMyMethod> PrepareTestCases_Param2()
    {
    返回PrepareTestCases(参数2);
    }


  3. 写的测试方法有通过paremeterless包装作为测试用例来源:

      [TestCaseSource(PrepareTestCases_Param1)] 
    公共无效TestRun1(ResultsOfCallMyMethod数据)
    {
    }

    [TestCaseSource(PrepareTestCases_Param2)]
    公共无效TestRun2(ResultsOfCallMyMethod数据)
    {
    }



I have the following method which generates a set of test cases!

public IEnumerable<ResultsOfCallMyMethod> PrepareTestCases(param1)
{
    foreach (string entry in entries)
    {
        yield return callMyMethod(param1);
    }
}

How can I pass param which is of type string as a parameter to my PrepareTestCases() method?

Is there a way to do the following:

[Test, Category("Integration"), TestCaseSource("PrepareTestCases", param1)]
public void TestRun(ResultsOfCallMyMethod testData)
{
    // do something!
}

解决方案

If you look at TestCaseSourceAttribute doc you will see there is no any way to pass the parameter to the method which returns test cases.

The method, which generates test cases, should be parameterless.

So, assuming you are going to avoid code duplication and you need to reuse the same method to generate a few test case lists, I'd recommend you to do the following:

  1. Write parameterized method which actually generates test cases sets:
    (PrepareTestCases() already does that)

    public IEnumerable<ResultsOfCallMyMethod> PrepareTestCases(string param)
    {
        foreach (string entry in entries)
        {
            yield return CallMyMethod(param);
        }
    }
    

  2. Write parameterless wrappers which call test cases generator and pass desired parameter there:

    public IEnumerable<ResultsOfCallMyMethod> PrepareTestCases_Param1()
    {
        return PrepareTestCases("param1");
    }
    
    public IEnumerable<ResultsOfCallMyMethod> PrepareTestCases_Param2()
    {
        return PrepareTestCases("param2");
    }
    

  3. Write test methods and pass paremeterless wrappers there as test case sources:

    [TestCaseSource("PrepareTestCases_Param1")]
    public void TestRun1(ResultsOfCallMyMethod data)
    {
    }
    
    [TestCaseSource("PrepareTestCases_Param2")]
    public void TestRun2(ResultsOfCallMyMethod data)
    {
    }
    

这篇关于C#NUnit的TestCaseSource传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆