[英]AutoFixture and XUnit2 - Combining TheoryData and AutoData
In the tests I'm currently writing I need to mix test-specific data with random entries that ideally I would like AutoFixture to generate.在我目前正在编写的测试中,我需要将特定于测试的数据与理想情况下我希望 AutoFixture 生成的随机条目混合。 I'm using TheoryData to create test-specific data set:
我正在使用 TheoryData 创建特定于测试的数据集:
internal sealed class TestSpecificData : TheoryData<int, string>
{
// Addition of test cases input.
}
In my XUnit2 test case I would like to do the following:在我的 XUnit2 测试用例中,我想做以下事情:
[Theory]
[AutoData]
[ClassData(TestSpecificData)]
public void MyTestCase(int first, string second, double third)
{
// Test scenario.
}
In the above test case I would like AutoFixture to generate only the last parameter: "double third" and use data available in TestSpecificData theory data for the first two parameters.在上面的测试用例中,我希望 AutoFixture 仅生成最后一个参数:“双三分之二”,并使用 TestSpecificData 理论数据中可用的数据作为前两个参数。 Attempting to run above code results in InvalidOperationException with following message:
尝试运行上述代码会导致 InvalidOperationException 并显示以下消息:
"The test method expected 3 parameter values, but 2 parameter values were provided."
I know I could use an InlineAutoData attribute, but there are a lot of test-specific entries and it would not be an ideal solution, since I would have to declare a separate InlineAutoData for each case.我知道我可以使用 InlineAutoData 属性,但是有很多特定于测试的条目,这不是一个理想的解决方案,因为我必须为每种情况声明一个单独的 InlineAutoData。 Moreover I would like to reuse test entries in different test cases, so this is why I'm attempting to use TheoryData.
此外,我想在不同的测试用例中重用测试条目,所以这就是我尝试使用 TheoryData 的原因。 I also tried to create a custom AutoFixture attribute that inherits from AutoDataAttribute, but I can't seem to make it work like I would like it to.
我还尝试创建一个继承自 AutoDataAttribute 的自定义 AutoFixture 属性,但我似乎无法让它像我希望的那样工作。 I would appreciate any suggestions or different solutions that would help me achieve desired outcome.
我将不胜感激任何可以帮助我实现预期结果的建议或不同的解决方案。
Thank you!谢谢!
Unfortunately there is currently no support for ClassDataAttribute
in AutoFixture.XUnit2
.不幸的是,目前
ClassDataAttribute
中不支持AutoFixture.XUnit2
。 However there is currently a pull request opened that adds this exact feature to AutoFixture.NUnit3
.但是,当前打开了一个拉取请求,将这个确切的功能添加到
AutoFixture.NUnit3
。 You could draw some inspiration from there.你可以从那里得到一些灵感。
If you manage to come up with an implementation please consider filing a pull request on the official AutoFixture repository.如果您设法提出实施方案,请考虑在AutoFixture官方存储库上提交拉取请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.