使用xmlUnit对XML字符串对比的一些技巧
2008-11-13 11:52
573 查看
今天又遭遇比较变态的 XML 字符串对比。所以特意将几种特殊的对比总结了一下:
1. 如何对比 XML 中某些特定路径节点是否相等,而不是整体对比。测试场景,如得到订购信息,可能订购的结束日期是实时生成的,假如我们不需要对比(每次回归都回出错)。那么参加如下脚本
String ExpXmlText ="<?xml version=\"1.0\" encoding=\"utf-8\"?><AppSubscResult>"+"\r\n"+
" <result>Yes</result>"+"\r\n"+
" <subscUserId>9950001</subscUserId>"+"\r\n"+
" <appInstanceId></appInstanceId>"+"\r\n"+
" <gmtStart>2008-08-11 00:00:00</gmtStart>"+"\r\n"+
" <gmtEnd>2008-10-10 23:59:59</gmtEnd>"+"\r\n"+
" <subscUrl></subscUrl>"+"\r\n"+
" <errorMessage></errorMessage>"+"\r\n"+
"</AppSubscResult>";
String path1 = "/AppSubscResult/result";
String path2 = "/AppSubscResult/subscUserId";
String path4 = "/AppSubscResult/gmtStart";
String path5 = "/AppSubscResult/gmtEnd";
String path6 = "/AppSubscResult/subscUrl";
String path7 = "/AppSubscResult/errorMessage";
XMLAssert.assertXpathValuesEqual(path1,ExpXmlText,path1,ActXmlText);
XMLAssert.assertXpathValuesEqual(path2,ExpXmlText,path2,ActXmlText);
XMLAssert.assertXpathValuesEqual(path4,ExpXmlText,path4,ActXmlText);
XMLAssert.assertXpathValuesEqual(path5,ExpXmlText,path5,ActXmlText);
XMLAssert.assertXpathValuesEqual(path6,ExpXmlText,path6,ActXmlText);
XMLAssert.assertXpathValuesEqual(path7,ExpXmlText,path7,ActXmlText);
2. 如果 xml 文件中含多个同节点路径的节点,如何只断言内容,而不关注节点次序。 测试场景如下,测试下面的期望结果,不关系 a 节点的排序情况。
<root>
<a>1</a>
<a>2</a>
<a>3</a>
</root>
String ActXmlText = response.getText();
System.out.println(ActXmlText);
Diff myDiff = new Diff(ExpXmlText, ActXmlText);
myDiff.overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(1));
XMLAssert.assertXMLEqual("GetTribMemberList 列表 ",
myDiff, true);
3 . 但是如果用 xmlunit1.1 的版本对于下面的 xml 字符串,如果期望附录 a 的节点的次序不一致的问题就无能为力了 ,那么用户就要升级 xmlunit 了,升级到 xmlunit1.2 的版本
<root>
<a><c>1.1</c><d>1.2</d></a>
<a><c>2.1</c><d>2.2</d></a>
</root>
代码如下:
Diff myDiff = new Diff(ExpXmlText, ActXmlText);
//RecursiveElementNameAndTextQualifier
myDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
XMLAssert.assertXMLEqual("GetTribChiefUser 列表 ",
myDiff, true);
1. 如何对比 XML 中某些特定路径节点是否相等,而不是整体对比。测试场景,如得到订购信息,可能订购的结束日期是实时生成的,假如我们不需要对比(每次回归都回出错)。那么参加如下脚本
String ExpXmlText ="<?xml version=\"1.0\" encoding=\"utf-8\"?><AppSubscResult>"+"\r\n"+
" <result>Yes</result>"+"\r\n"+
" <subscUserId>9950001</subscUserId>"+"\r\n"+
" <appInstanceId></appInstanceId>"+"\r\n"+
" <gmtStart>2008-08-11 00:00:00</gmtStart>"+"\r\n"+
" <gmtEnd>2008-10-10 23:59:59</gmtEnd>"+"\r\n"+
" <subscUrl></subscUrl>"+"\r\n"+
" <errorMessage></errorMessage>"+"\r\n"+
"</AppSubscResult>";
String path1 = "/AppSubscResult/result";
String path2 = "/AppSubscResult/subscUserId";
String path4 = "/AppSubscResult/gmtStart";
String path5 = "/AppSubscResult/gmtEnd";
String path6 = "/AppSubscResult/subscUrl";
String path7 = "/AppSubscResult/errorMessage";
XMLAssert.assertXpathValuesEqual(path1,ExpXmlText,path1,ActXmlText);
XMLAssert.assertXpathValuesEqual(path2,ExpXmlText,path2,ActXmlText);
XMLAssert.assertXpathValuesEqual(path4,ExpXmlText,path4,ActXmlText);
XMLAssert.assertXpathValuesEqual(path5,ExpXmlText,path5,ActXmlText);
XMLAssert.assertXpathValuesEqual(path6,ExpXmlText,path6,ActXmlText);
XMLAssert.assertXpathValuesEqual(path7,ExpXmlText,path7,ActXmlText);
2. 如果 xml 文件中含多个同节点路径的节点,如何只断言内容,而不关注节点次序。 测试场景如下,测试下面的期望结果,不关系 a 节点的排序情况。
<root>
<a>1</a>
<a>2</a>
<a>3</a>
</root>
String ActXmlText = response.getText();
System.out.println(ActXmlText);
Diff myDiff = new Diff(ExpXmlText, ActXmlText);
myDiff.overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(1));
XMLAssert.assertXMLEqual("GetTribMemberList 列表 ",
myDiff, true);
3 . 但是如果用 xmlunit1.1 的版本对于下面的 xml 字符串,如果期望附录 a 的节点的次序不一致的问题就无能为力了 ,那么用户就要升级 xmlunit 了,升级到 xmlunit1.2 的版本
<root>
<a><c>1.1</c><d>1.2</d></a>
<a><c>2.1</c><d>2.2</d></a>
</root>
代码如下:
Diff myDiff = new Diff(ExpXmlText, ActXmlText);
//RecursiveElementNameAndTextQualifier
myDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
XMLAssert.assertXMLEqual("GetTribChiefUser 列表 ",
myDiff, true);
相关文章推荐
- 使用xmlUnit对XML字符串对比的一些技巧
- XML字符串对比技巧二
- js字符串转换成xml对象并使用技巧解读
- 分享dos批处理命令的一些使用技巧(截取字符串)
- XML字符串对比技巧二
- 使用Visual Studio 快速把 Json,Xml 字符串创建为一个实体类
- 使用PHP函数写验证码截取一些字符串
- iOS开发UI篇—IOS开发中Xcode的一些使用技巧
- centos 下安装vim 及vim的一些使用技巧
- mongodb一些使用技巧或注意事项记录
- 虚幻引擎UE4-命令行使用的一些技巧
- Backbone.js的一些使用技巧
- android TextView EditTextView一些技巧使用 (视图代码布局)
- linux命令之目录监控 字符串拆分 监控命令运行结果 统计行数 去除文本头尾空格 获取网卡名浮点数运算等命令使用技巧
- [前端]LocalResizeImg的使用技巧、与Struts2的结合以及一些坑点
- Linq to XML使用技巧
- 以下一些使用ASP.NET和VISUAL STUDIO.NET2003的经验和技巧
- js 使用技巧的一些技巧
- Android 字符串转换String.format使用及String使用技巧