在单元测试中验证XML的最佳方法是? [英] The best way to validate XML in a unit test?

查看:86
本文介绍了在单元测试中验证XML的最佳方法是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ToString方法生成XML的类.我想对其进行单元测试,以确保它产生有效的xml.我有一个DTD来验证XML.

I have a class with a ToString method that produces XML. I want to unit test it to ensure it is producing valid xml. I have a DTD to validate the XML against.

我是否应该将DTD作为字符串包含在单元测试中,以避免对它的依赖,或者有更聪明的方法吗?

Should I include the DTD as a string within the unit test to avoid a dependency on it, or is there a smarter way to do this?

推荐答案

如果您的程序在正常执行过程中针对DTD验证了XML,那么您应该只从程序可以获取DTD的任何地方获取它.

If your program validates the XML against the DTD during normal execution, then you should just get the DTD from wherever your program will get it.

如果没有,并且DTD非常短(只有几行),那么将其作为字符串存储在代码中就可以了.

If not and the DTD is extremely short (only a few lines), then storing it as a string in your code is probably okay.

否则,我会将其放在一个外部文件中,并让您的单元测试从该文件中读取它.

Otherwise, I'd put it in an external file and have your unit test read it from that file.

这篇关于在单元测试中验证XML的最佳方法是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆