这些天我试图了解 xUnit 测试的工作原理,特别是,我发现有 3 种方法将数据作为参数传递以测试类方法(InlineData、ClassData 和 MemberData)。但这是我的问题:是否有机会从外部文件获取这些数据? (例如 Json 文件)
我无法找到有关该主题的足够材料,感谢您的关注!
xUnit 被设计为可扩展的,即通过数据属性 https://github.com/xunit/xunit/blob/master/src/xunit.core/Sdk/DataAttribute.cs.
内联数据 https://github.com/xunit/xunit/blob/master/src/xunit.core/InlineDataAttribute.cs, 类数据 https://github.com/xunit/xunit/blob/master/src/xunit.core/ClassDataAttribute.cs and 会员资料 https://github.com/xunit/xunit/blob/master/src/xunit.core/MemberDataAttribute.cs全部源自DataAttribute
,您可以扩展自己来为数据理论创建自定义数据源,您可以在其中读取外部文件并使用例如Json.NET https://www.newtonsoft.com/json反序列化您的数据。
User Sock https://stackoverflow.com/users/6375486/sock在他的博客中写到了这一点JSON,正如您提到的:
- 创建自定义 xUnit 理论测试 DataAttribute 以从 JSON 文件加载数据 https://andrewlock.net/creating-a-custom-xunit-theory-test-dataattribute-to-load-data-from-json-files/
- GitHub 上的源代码 https://github.com/andrewlock/blog-examples/tree/master/XUnitTheoryTests/XUnitTheoryTests
与数据相关的问题CSV file:
如何使用 CSV 文件中的数据运行 XUnit 测试 https://stackoverflow.com/questions/42727394/how-to-run-xunit-test-using-data-from-a-csv-file
这里有两个xUnit样品:
- Excel数据 https://github.com/xunit/samples.xunit/tree/0c8ef4e4291b0ddc98469ad1317d3ad0c7904ce7/ExcelDataExample
- SqlData https://github.com/xunit/samples.xunit/tree/0c8ef4e4291b0ddc98469ad1317d3ad0c7904ce7/SqlDataExample
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)