用于单元测试的 .net Xml 比较器
我有一些单元测试,需要确保方法生成的 XML 包含与预期 Xml 文档相同的元素/值。
我在 Java 中使用了 xmlunit ,尽管它们有 .net 版本,但它似乎不支持命名空间。 .net 中是否有其他替代方案可以实现此目的?
只要我可以比较 2 个 Xml 字符串并获得一个真/假结果来告诉我它们是否与所包含的数据匹配,我就很高兴......
I have a few unit tests where I need to make sure that XML generated by a method contains the same elements/values as an expected Xml document.
I used xmlunit in Java, and although they have a .net version it doesn't seem to support namespaces. Are there any alternatives within .net for doing this?
As long as I can just compare 2 Xml strings and get a true/false result to tell me if they match as far as the data contained is concerned I am happy...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通常发现 XNode.DeepEquals 足以满足我的需求。它是 BCL 的一部分,因此无需下载。
I've usually found that XNode.DeepEquals is sufficient for my needs. It's part of the BCL, so no download is required.
尝试 Microsoft.XmlDiffPatch:
Try Microsoft.XmlDiffPatch:
关于 MSXML XMLDiff 需要记住的一点是,如果您要比较非常大的 XML 文档,请确保您的
XMLDiff.Algorithm
未设置为“Precise”,否则您可能会运行内存不足。默认情况下,其设置为“自动”,这是一个安全的选择,因为 API 将根据文件大小、检测到的差异数量和其他因素选择是使用“精确”还是“快速”。对于更倾向于技术的人来说,这是一个很好的阅读:http://treepatch.sourceforge.net/report.pdf< /a>
Something to keep in mind about MSXML XMLDiff is that if you are comparing very large XML documents be sure your
XMLDiff.Algorithm
is not set to "Precise" or you will potentially run out of memory. By default its set to Auto which is a safe choice as the API will choose whether to use Precise or Fast based on the file size, number of detected differences and other factors. Here is a good read for the more technically inclined:http://treepatch.sourceforge.net/report.pdf
我过去曾使用过 MS 的 XMLDiff,但更喜欢使用 Beyond Compare 3,因为它具有更好的 GUI 和批处理功能(尽管没有 .NET API)。
为了进行测试,请使用 XNode.DeepEquals 或 InnerXML 来比较基于字符串的表示
I have used MS's XMLDiff is the past, but preferred to use Beyond Compare 3's as it has better GUI and batch processing feature (doenst have .NET API though).
for your testing, use XNode.DeepEquals or InnerXML to compare string based representation