【问题标题】:AutoFixture and XUnit2 - Combining TheoryData and AutoDataAutoFixture 和 XUnit2 - 结合 TheoryData 和 AutoData
【发布时间】:2021-08-25 07:36:04
【问题描述】:

在我目前正在编写的测试中,我需要将特定于测试的数据与理想情况下我希望 AutoFixture 生成的随机条目混合。 我正在使用 TheoryData 创建特定于测试的数据集:

internal sealed class TestSpecificData : TheoryData<int, string>
{
    // Addition of test cases input.
}

在我的 XUnit2 测试用例中,我想做以下事情:

[Theory]
[AutoData]
[ClassData(TestSpecificData)]
public void MyTestCase(int first, string second, double third)
{
    // Test scenario.
}

在上面的测试用例中,我希望 AutoFixture 只生成最后一个参数:“双三分之二”,并使用 TestSpecificData 理论数据中可用的数据作为前两个参数。 尝试运行上述代码会导致 InvalidOperationException 并显示以下消息:

"The test method expected 3 parameter values, but 2 parameter values were provided."

我知道我可以使用 InlineAutoData 属性,但是有很多特定于测试的条目,这不是一个理想的解决方案,因为我必须为每种情况声明一个单独的 InlineAutoData。此外,我想在不同的测试用例中重用测试条目,所以这就是我尝试使用 TheoryData 的原因。 我还尝试创建一个继承自 AutoDataAttribute 的自定义 AutoFixture 属性,但我似乎无法让它像我希望的那样工作。 如果有任何建议或不同的解决方案可以帮助我达到预期的结果,我将不胜感激。

谢谢!

【问题讨论】:

    标签: c# unit-testing testing xunit autofixture


    【解决方案1】:

    很遗憾,AutoFixture.XUnit2 中目前不支持 ClassDataAttribute。然而,目前打开了一个pull request,它将这个确切的功能添加到AutoFixture.NUnit3。你可以从中汲取灵感。

    如果您设法提出实施方案,请考虑在官方 AutoFixture 存储库上提交拉取请求。

    【讨论】:

    • 感谢您的回答。有点遗憾,但看来我必须找到解决方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多