使用测试工具进行HTTP测试。

简介:
在选择什么样的测试工具之前,需要分析在实际工作工程中有哪几种请求类型需要模拟,然后根据实际测试需要选择合适的工具。在这里工具不一定是一种,可以根据实际的场景来选择,甚至几种工具组合使用。通常情况下需要模拟的消息请求场景有以下几种。
1) 最简单的请求,请求访问某个网页。
2) 通过Get方法访问页面并且加入参数。
3) 通过Post方法访问页面并且加入参数
4) 通过Post方法访问页面并且需要提交表单
同样如果对验证点进行总结,大致也有以下几个方面的内容需要进行验证。
1)      原因短语字段
2)      网页的内容(包含链接,图片)
3)      服务器处理结果验证(包含返回的Soap消息,sessionId等)
    本书将主要几个测试案例来说明如何使用HTTPUNIT和XMLUNIT来实现以上几种消息请求的模拟和结果的验证。
场景1:访问" http://www.baidu.com",并且验证相关的消息头和检验网页中的WebLink是否符合期望。代码如下
01    import org.junit.Test;
02    import static org.junit.Assert.*;
03    import com.meterware.httpunit.WebConversation;
04    import com.meterware.httpunit.WebResponse;
05    @Test
06    public void TestCase1() throws Exception
07    {
08           System.out.println("直接获取网页内容:");
09           //建立一个WebConversation实例
10           WebConversation wc = new WebConversation();
11           //向指定的URL发出请求,获取响应
12           WebResponse wr = wc.getResponse( " http://www.baidu.com" );
13           //期望返回消息头
14           String[] Heads={"CP=\" OTI DSP COR IVA OUR IND COM \"","Sat, 15 Mar 2008 09:54:00 GMT"
15                         ,"BWS/1.0","BAIDUID=ACBF37FA63FA39B866C6F4C190E96845:FG=1; expires=Sat, 15-Mar-38 09:54:00 GMT; path=/; domain=.baidu.com"
16                         ,"text/html","gzip","Sat, 15 Mar 2008 09:54:00 GMT","1559","private"};
 

01    import org.junit.Test;
02    import static org.junit.Assert.*;
03    import com.meterware.httpunit.WebConversation;
04    import com.meterware.httpunit.WebResponse;
05    @Test
06    public void TestCase1() throws Exception
07    {
08           System.out.println("直接获取网页内容:");
09           //建立一个WebConversation实例
10           WebConversation wc = new WebConversation();
11           //向指定的URL发出请求,获取响应
12           WebResponse wr = wc.getResponse( " http://www.baidu.com" );
13           //期望返回消息头
14           String[] Heads={"CP=\" OTI DSP COR IVA OUR IND COM \"","Sat, 15 Mar 2008 09:54:00 GMT"
15                         ,"BWS/1.0","BAIDUID=ACBF37FA63FA39B866C6F4C190E96845:FG=1; expires=Sat, 15-Mar-38 09:54:00 GMT; path=/; domain=.baidu.com"
16                         ,"text/html","gzip","Sat, 15 Mar 2008 09:54:00 GMT","1559","private"};
 

17           String s1 = wr.getHeaderFieldNames()[0];
18           System.out.println(wr.getHeaderField(s1));
19           assertEquals(Heads[0],wr.getHeaderField(s1));
20           s1 = wr.getHeaderFieldNames()[2];
21           assertEquals(Heads[2],wr.getHeaderField(s1));
22           //准备期望的网页链接
23           String[] Explinks ={
24                         " http://hi.baidu.com/baidu",
25                         " http://news.baidu.com",
26                         " http://tieba.baidu.com",
27                         " http://zhidao.baidu.com",
28                         " http://mp3.baidu.com",
29                         " http://image.baidu.com",
30                         "/search/jiqiao.html",
31                         "/gaoji/advanced.html",
32                         " http://hi.baidu.com",
33                         " http://www.baidu.com/more",
34                         " http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com",
35                         " http://jingjia.baidu.com",
36                         " http://top.baidu.com",
37                         "/home.html",
38                         " http://ir.baidu.com",
39                         " http://www.baidu.com/duty",
40                         " http://www.miibeian.gov.cn",
41                         " http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412"
42           };
43           //对网页上所有的链接进行断言
44           for(int i = 0; i <= wr.getLinks().length -1; i++)
45           {
46                  assertEquals(Explinks[i],wr.getLinks()[i].getURLString());
47                  System.out.println(wr.getLinks()[i].getURLString());
48           }
49    }
17           String s1 = wr.getHeaderFieldNames()[0];
18           System.out.println(wr.getHeaderField(s1));
19           assertEquals(Heads[0],wr.getHeaderField(s1));
20           s1 = wr.getHeaderFieldNames()[2];
21           assertEquals(Heads[2],wr.getHeaderField(s1));
22           //准备期望的网页链接
23           String[] Explinks ={
24                         " http://hi.baidu.com/baidu",
25                         " http://news.baidu.com",
26                         " http://tieba.baidu.com",
27                         " http://zhidao.baidu.com",
28                         " http://mp3.baidu.com",
29                         " http://image.baidu.com",
30                         "/search/jiqiao.html",
31                         "/gaoji/advanced.html",
32                         " http://hi.baidu.com",
33                         " http://www.baidu.com/more",
34                         " http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com",
35                         " http://jingjia.baidu.com",
36                         " http://top.baidu.com",
37                         "/home.html",
38                         " http://ir.baidu.com",
39                         " http://www.baidu.com/duty",
40                         " http://www.miibeian.gov.cn",
41                         " http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412"
42           };
43           //对网页上所有的链接进行断言
44           for(int i = 0; i <= wr.getLinks().length -1; i++)
45           {
46                  assertEquals(Explinks[i],wr.getLinks()[i].getURLString());
47                  System.out.println(wr.getLinks()[i].getURLString());
48           }
49    }
 

     测试代码01-04说明本用例需要的jar包。由于HTTPUNIT是由junit扩展的,读者在编写测试代码的时候也可以直接从HttpUnitTest扩展,但是这样不是特别灵活,会造成其他框架诸如XmlUnit等无法使用,所以建议还是从junit扩展,然后使用其中功能即可。
 
    场景2:访问 www.mytest.com 跳转到 www.mylogin.com,添加参数用户名密码,返回到 www.mytest.com。并且验证放回的soap消息中的内容是否正确,如果成功登陆那么返回消息”Success”。测试代码如代码5.2
01    @Test
02    public void TestCase8() throws Exception
03    {
04           String sip_appkey = "100";//app_id
05         String sip_apiname = "ali.ali-7695-sip";
06         String sip_appsecret = "test_secret";
07         //跳转到的登陆页面
08         String api_server = " http://www.mylogin.com";
09         //第一次访问页面
10         String url = " http://www.mytest.com/";
11          //先登第一次要访问的页面,目的获得测试的Post
12           WebConversation conversation = new WebConversation();
13           WebRequest request = new PostMethodWebRequest(url);
14           WebResponse response = conversation.getResponse(request);
15          WebForm testForm = response.getForms()[0];
16          request = testForm.getRequest();
17          //添加第一页面需要的参数
18          request.setParameter("sip_appkey", sip_appkey);
19          request.setParameter("sip_apiname", sip_apiname);
20          request.setParameter("sip_appsecret", sip_appsecret);
21          request.setParameter("api_server", api_server);
22          response = conversation.getResponse(request);
23          System.out.println(response.getText());
24          //由于需要登陆才能访问所以转到登陆页面,
25          //登陆页面链接有传递的api_server地址和相关参数在服务器端经过计算返回给response
26          WebForm loginForm = response.getForms()[0];
27          request = loginForm.getRequest();
29          request.setParameter("username", "Sip");
30          request.setParameter("pwd", "1");
31         
32          response = conversation.getResponse(request);
33          System.out.println(response.getText());
34          String XmlTest = response.getText();
35          if (XmlTest.indexOf("Success") >=0)
37                  assertTrue(true);
38           else
39                  assertTrue(false);
40    }
 

01    @Test
02    public void TestCase8() throws Exception
03    {
04           String sip_appkey = "100";//app_id
05         String sip_apiname = "ali.ali-7695-sip";
06         String sip_appsecret = "test_secret";
07         //跳转到的登陆页面
08         String api_server = " http://www.mylogin.com";
09         //第一次访问页面
10         String url = " http://www.mytest.com/";
11          //先登第一次要访问的页面,目的获得测试的Post
12           WebConversation conversation = new WebConversation();
13           WebRequest request = new PostMethodWebRequest(url);
14           WebResponse response = conversation.getResponse(request);
15          WebForm testForm = response.getForms()[0];
16          request = testForm.getRequest();
17          //添加第一页面需要的参数
18          request.setParameter("sip_appkey", sip_appkey);
19          request.setParameter("sip_apiname", sip_apiname);
20          request.setParameter("sip_appsecret", sip_appsecret);
21          request.setParameter("api_server", api_server);
22          response = conversation.getResponse(request);
23          System.out.println(response.getText());
24          //由于需要登陆才能访问所以转到登陆页面,
25          //登陆页面链接有传递的api_server地址和相关参数在服务器端经过计算返回给response
26          WebForm loginForm = response.getForms()[0];
27          request = loginForm.getRequest();
29          request.setParameter("username", "Sip");
30          request.setParameter("pwd", "1");
31         
32          response = conversation.getResponse(request);
33          System.out.println(response.getText());
34          String XmlTest = response.getText();
35          if (XmlTest.indexOf("Success") >=0)
37                  assertTrue(true);
38           else
39                  assertTrue(false);
40    }
 
    以上两个场景都侧重描述如何模拟消息的请求,而消息的验证都使用字符串对比的方法,而实际上现在很多测试工具都提供了现成的断言方法,并且可以对结果加以分析,使得测试结果更加直观。如对Soap消息中Xml文本内容的对比方法就有XMLUNIT等工具可以实现。限于篇幅本书只引摘Xmlunit中的例子来做简要说明其方法,读者有兴趣可以做深入研究。
01    import java.io.File;
02    import java.io.FileReader;
03    import java.util.List;
04   
05    import javax.xml.transform.stream.StreamSource;
06   
07    import org.w3c.dom.Document;
08    import org.w3c.dom.Element;
09    import org.w3c.dom.Node;
10    import org.w3c.dom.Text;
11   
12    import org.custommonkey.xmlunit.*;
13    public class MyXMLTestCase extends XMLTestCase {
14        public MyXMLTestCase(String name) {
15            super(name);
16        }
17                  //判断两个xml文档是否相同
18        public void testForEquality() throws Exception {
19            String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>";
20            String myTestXML = "<msg><localId>2376</localId></msg>";
21            assertXMLEqual("comparing test xml to control xml", myControlXML, myTestXML);
22   
23            assertXMLNotEqual("test xml not similar to control xml", myControlXML, myTestXML);
24        }
25                  //例举文档中相同的地方和不同的地方
26        public void testIdentical() throws Exception {
27            String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";
28            String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";
29            Diff myDiff = new Diff(myControlXML, myTestXML);
30            assertTrue("pieces of XML are similar " + myDiff, myDiff.similar());
31            assertTrue("but are they identical? " + myDiff, myDiff.identical());
32        }
 

01    import java.io.File;
02    import java.io.FileReader;
03    import java.util.List;
04   
05    import javax.xml.transform.stream.StreamSource;
06   
07    import org.w3c.dom.Document;
08    import org.w3c.dom.Element;
09    import org.w3c.dom.Node;
10    import org.w3c.dom.Text;
11   
12    import org.custommonkey.xmlunit.*;
13    public class MyXMLTestCase extends XMLTestCase {
14        public MyXMLTestCase(String name) {
15            super(name);
16        }
17                  //判断两个xml文档是否相同
18        public void testForEquality() throws Exception {
19            String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>";
20            String myTestXML = "<msg><localId>2376</localId></msg>";
21            assertXMLEqual("comparing test xml to control xml", myControlXML, myTestXML);
22   
23            assertXMLNotEqual("test xml not similar to control xml", myControlXML, myTestXML);
24        }
25                  //例举文档中相同的地方和不同的地方
26        public void testIdentical() throws Exception {
27            String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";
28            String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";
29            Diff myDiff = new Diff(myControlXML, myTestXML);
30            assertTrue("pieces of XML are similar " + myDiff, myDiff.similar());
31            assertTrue("but are they identical? " + myDiff, myDiff.identical());
32        }
 

本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/282648,如需转载请自行联系原作者
相关文章
|
4月前
|
JavaScript 前端开发 安全
在众多的测试工具中,Cypress以其强大的端到端测试能力和与TypeScript的完美结合,成为了前端开发者的首选
【6月更文挑战第11天】Cypress结合TypeScript,打造前端测试新体验。TypeScript增强代码可读性和稳定性,Cypress提供强大端到端测试,二者结合提升测试准确性和可靠性。通过类型定义、自定义命令和断言,优化测试代码;Cypress模拟真实用户操作、时间旅行功能及内置调试工具,确保应用功能性能。推荐前端开发者使用TypeScript+Cypress进行端到端测试。
59 2
|
23天前
|
JSON 移动开发 监控
快速上手|HTTP 接口功能自动化测试
HTTP接口功能测试对于确保Web应用和H5应用的数据正确性至关重要。这类测试主要针对后台HTTP接口,通过构造不同参数输入值并获取JSON格式的输出结果来进行验证。HTTP协议基于TCP连接,包括请求与响应模式。请求由请求行、消息报头和请求正文组成,响应则包含状态行、消息报头及响应正文。常用的请求方法有GET、POST等,而响应状态码如2xx代表成功。测试过程使用Python语言和pycurl模块调用接口,并通过断言机制比对实际与预期结果,确保功能正确性。
101 3
快速上手|HTTP 接口功能自动化测试
|
5月前
|
安全 Java 测试技术
【软件测试】测试工具推荐
【软件测试】测试工具推荐
|
2月前
|
测试技术 API
软件测试:Postman 工具的使用。开发及测试均需要掌握的测试工具
这篇文章详细介绍了Postman工具的各个模块功能,包括创建请求、集合、环境、自动化测试等,并解释了如何使用Postman进行GET、POST、PUT和DELETE等常见HTTP请求的测试。
|
2月前
|
JSON jenkins 测试技术
Python接口自动化测试框架(工具篇)-- 接口测试工具HTTPRUNNER
本文介绍了Python接口自动化测试框架HTTPRunner,包括其安装、使用方法,并通过实际操作演示了如何利用HTTPRunner进行接口测试,同时还探讨了HTTPRunner作为接口自动化测试解决方案的可能性和实用性。
44 0
|
3月前
|
数据采集 缓存 负载均衡
实测 | 芝麻代理,快代理、熊猫代理、豌豆代理HTTP代理质量测试
哈喽大家,欢迎来到本期知识分享!我们将探讨HTTP代理的质量分析方法,无论新手还是资深用户都能从中受益。首先介绍了HTTP代理的基本概念及其重要性。接着,我们通过两个关键指标——响应时间和可用性来评估代理质量。响应时间可通过`curl`命令测试并计算平均值;可用性则需设置定时任务持续检测,比如使用Python脚本。最后,通过具体案例分析了几家知名代理供应商的表现,其中青果网络在各项指标上表现突出,是进行数据采集等活动的优质选择。记得选择最适合自己的代理服务哦!
实测 | 芝麻代理,快代理、熊猫代理、豌豆代理HTTP代理质量测试
|
3月前
|
测试技术 Python
我们假设要测试一个名为`http://example.com`的网站,并对其进行简单的GET请求性能测试。
我们假设要测试一个名为`http://example.com`的网站,并对其进行简单的GET请求性能测试。
|
3月前
|
Shell Python
`pytest-httpserver`是一个pytest插件,它允许你在测试期间启动一个轻量级的HTTP服务器,并模拟HTTP请求和响应。
`pytest-httpserver`是一个pytest插件,它允许你在测试期间启动一个轻量级的HTTP服务器,并模拟HTTP请求和响应。
|
3月前
|
JavaScript 测试技术
vue 官方测试工具 unit-jest 实用教程(含实战范例:登录组件的测试)
vue 官方测试工具 unit-jest 实用教程(含实战范例:登录组件的测试)
65 0
|
5月前
|
机器学习/深度学习 人工智能 测试技术
提升软件测试效率:智能化测试工具的应用与展望
【5月更文挑战第19天】 在快速发展的软件行业中,保证产品质量的同时提高测试效率已成为一个关键挑战。传统的手动测试方法由于其耗时且易出错的局限性,逐渐不能满足现代软件开发的需求。智能化测试工具的出现为解决这一问题提供了新的思路。本文将探讨智能化测试工具如何通过自动化和人工智能技术优化测试流程,减少重复性工作,并预测未来测试工具的发展趋势。我们将分析这些工具在实际应用中的表现,以及它们对提高软件测试效率和准确性的潜在影响。
119 8
下一篇
无影云桌面