简体   繁体   English

AutoFixture 和 XUnit2 - 结合 TheoryData 和 AutoData

[英]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.

相关问题 具有参数的AutoFixture AutoData - AutoFixture AutoData with arguments 使用NUnit和AutoData的AutoFixture会抛出TargetParameterCountException - AutoFixture with NUnit and AutoData throws TargetParameterCountException 您可以在AutoFixture中将内联值与AutoData结合吗? - Can you combine inlined values with AutoData in AutoFixture? AutoFixture - 创建“有效”和“无效”实例和 [AutoData] - AutoFixture - Creation of “Valid” and “Invalid” instances and [AutoData] 如何将基于约定的自定义项与AutoFixture的[AutoData]属性结合在一起? - How to combine a Convention-based Customization with AutoFixture's [AutoData] attribute? 使用 AutoFixture 和 AutoData 时如何生成默认的 UTC DateTime? - How to generate default UTC DateTime when using AutoFixture and AutoData? 来自 Autofixture 声明性自动数据参数的属性的集合大小 - Collection size from attribute for Autofixture declarative autodata parameter AutoFixture将PropertyData与多个条目和AutoData混合(使用AutoMoqCustomization) - AutoFixture mixing PropertyData with multiple entries and AutoData (using AutoMoqCustomization) XUnit,AutoFixture和Moq最佳实践 - XUnit, AutoFixture and Moq best practice 我可以在XUnit构造函数中自定义Fixture以便与Theory和AutoData一起使用吗? - Can I customise a Fixture in an XUnit constructor for use with Theory and AutoData?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM