如何在一个N-Unit测试中使用多个TestCaseSource属性。

问题描述 投票:11回答:1

如何在N-Unit 2.62中使用多个TestCaseSource属性为测试提供测试数据?

我目前正在做以下工作。

[Test, Combinatorial, TestCaseSource(typeof(FooFactory), "GetFoo"), TestCaseSource(typeof(BarFactory), "GetBar")]
FooBar(Foo x, Bar y)
{
 //Some test runs here.
}

我的测试案例数据源是这样的:

internal sealed class FooFactory
{
    public IEnumerable<Foo> GetFoo()
    {
        //Gets some foos.
    }
}


    internal sealed class BarFactory
{
    public IEnumerable<Bar> GetBar()
    {
        //Gets some bars.
    }
}

不幸的是,N-Unit甚至不能启动测试 因为它说我提供了错误的参数数量。我知道你可以指定一个TestCaseObject作为返回类型,并传入一个对象数组,但我认为这种方法是可行的。

你能帮我解决这个问题吗?

c# .net unit-testing nunit testcasesource
1个回答
14
投票

这种情况下适合使用的属性是 ValueSource. 本质上,你是在为每个参数指定一个数据源,就像这样。

public void TestQuoteSubmission(
    [ValueSource(typeof(FooFactory), "GetFoo")] Foo x, 
    [ValueSource(typeof(BarFactory), "GetBar")] Bar y)
{
    // Your test here.
}

这将使我所寻找的功能类型能够通过使用 TestCaseSource 属性。

© www.soinside.com 2019 - 2024. All rights reserved.