c# - 如何为 N 单元测试使用多个 TestCaseSource 属性

标签 c# .net unit-testing nunit testcasesource

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

我目前正在做以下事情:

[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 作为返回类型并传入一个对象数组,但我认为这种方法是可行的。

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

最佳答案

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

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

这将启用我使用 TestCaseSource 属性寻找的功能类型。

关于c# - 如何为 N 单元测试使用多个 TestCaseSource 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16346903/

相关文章:

c# - 结合编码的 UI 测试 HTML 日志?

swift - 快速灵活的变量始终为零

c# - 使用 HttpWebResponse 读取 "chunked"响应

c# - 无法在 C#/ASP.NET 中调试静态 Web 方法

.net - 是否有适用于 .NET 的 HtmlUnitDriver?

c# - 什么时候使用带有 xsl 的 xml 而不是 HTML?

reactjs - 如何使用 jest 和 RTL 模拟来自 react 组件的异步操作调用

java - 如何使用枚举参数对方法进行单元测试?

c# - 使用 C# 一般压扁 Json

c# - 转换为 ASPNET Core 3.1 后 MVC 找不到 View