传递复杂参数[理论] [英] Pass complex parameters to [Theory]

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

问题描述

的xUnit有一个很好的功能,你可以创建一个理论参数一次测试,并把数据InlineData'参数和的xUnit将generete多次试验和测试这一切 - > <一个href=\"http://stackoverflow.com/questions/9110419/test-parameterization-in-xunit-net-similar-to-nunit\">Link示例

我想有这样的事情,但是参数我的方法不是简单的数据(如字符串,整数,双),但是我的列表类的:

 公共静态无效WriteReportsToMemoryStream(IEnumerable的&LT; MyCustomClass&GT; listReport,MemoryStream的MS,StreamWriter的作家)
{
}


解决方案

有许多 xxxxData 属性中的xUnit。检查出例如 PropertyData 属性。

您可以实现returne属性的IEnumerable&LT;对象[]&GT; 。每个 [对象] ,这种方法生成会那么解包作为一个单一的呼叫参数的 [理论] 方法。

另一种选择是 ClassData ,其工作方式是相同的,但允许方便地共享在不同的类/命名空间的测试之间的发电机,并且还分离数据发生器'从实际的测试方法。

请参阅即从这里这些例子:

PropertyData示例

 公共类StringTests2
{
    [理论,PropertyData(SplitCountData)]
    公共无效SplitCount(字符串输入,INT expectedCount)
    {
        VAR actualCount = input.Split('').Count之间的();
        Assert.Equal(expectedCount,actualCount);
    }    公共静态的IEnumerable&LT;对象[]&GT; SplitCountData
    {
        得到
        {
            //或者这可以从文件中读取。 :)
            返回新的[]
            {
                新的对象[] {的xUnit,1},
                新的对象[] {很有趣,2},
                新的对象[] {与测试,3}
            };
        }
    }
}

ClassData示例

 公共类StringTests3
{
    [理论ClassData(typeof运算(IndexOfData))]
    公共无效的IndexOf(字符串输入,字符字母,INT预期)
    {
        VAR实际= input.IndexOf(函);
        Assert.Equal(预期,实际值);
    }
}公共类IndexOfData:IEnumerable的&LT;对象[]&GT;
{
    私人只读表&LT;对象[]&GT; _data =新的List&LT;对象[]&GT;
    {
        新的对象[] {世界你好,W,6},
        新的对象[] {晚安,月亮,W,-1}
    };    公众的IEnumerator&LT;对象[]&GT;的GetEnumerator()
    {返回_data.GetEnumerator(); }    的IEnumerator IEnumerable.GetEnumerator()
    {返回的GetEnumerator(); }
}

Xunit have a nice feature, that you can create one test with 'theory' parameter, and put data in 'InlineData' parameter and xUnit will generete many tests, and test it all -> Link to example

I want to have something like this, but parameter to my method is not 'simple data' (like string, int, double), but list of my class:

public static void WriteReportsToMemoryStream(IEnumerable<MyCustomClass> listReport, MemoryStream ms, StreamWriter writer)
{
}

解决方案

There are many xxxxData attributes in XUnit. Check out for example PropertyData attribute.

You can implement a property that returne IEnumerable<object[]>. Each of object[] that this method generates will be then "unpacked" as a parameters for a single call to your [Theory] method.

Another option is ClassData, which works the same, but allows to easily share the 'generators' between tests in different classes/namespaces, and also separates the 'data generators' from the actual test methods.

See i.e. these examples from here:

PropertyData Example

public class StringTests2
{
    [Theory, PropertyData("SplitCountData")]
    public void SplitCount(string input, int expectedCount)
    {
        var actualCount = input.Split(' ').Count();
        Assert.Equal(expectedCount, actualCount);
    }

    public static IEnumerable<object[]> SplitCountData
    {
        get
        {
            // Or this could read from a file. :)
            return new[]
            {
                new object[] { "xUnit", 1 },
                new object[] { "is fun", 2 },
                new object[] { "to test with", 3 }
            };
        }
    }
}

ClassData Example

public class StringTests3
{
    [Theory, ClassData(typeof(IndexOfData))]
    public void IndexOf(string input, char letter, int expected)
    {
        var actual = input.IndexOf(letter);
        Assert.Equal(expected, actual);
    }
}

public class IndexOfData : IEnumerable<object[]>
{
    private readonly List<object[]> _data = new List<object[]>
    {
        new object[] { "hello world", 'w', 6 },
        new object[] { "goodnight moon", 'w', -1 }
    };

    public IEnumerator<object[]> GetEnumerator()
    { return _data.GetEnumerator(); }

    IEnumerator IEnumerable.GetEnumerator()
    { return GetEnumerator(); }
}

这篇关于传递复杂参数[理论]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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