使用测试工具进行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,如需转载请自行联系原作者
相关文章
|
6月前
|
Java 测试技术 容器
Jmeter工具使用:HTTP接口性能测试实战
希望这篇文章能够帮助你初步理解如何使用JMeter进行HTTP接口性能测试,有兴趣的话,你可以研究更多关于JMeter的内容。记住,只有理解并掌握了这些工具,你才能充分利用它们发挥其应有的价值。+
987 23
|
10月前
|
JSON 前端开发 测试技术
大前端之前端开发接口测试工具postman的使用方法-简单get接口请求测试的使用方法-简单教学一看就会-以实际例子来说明-优雅草卓伊凡
大前端之前端开发接口测试工具postman的使用方法-简单get接口请求测试的使用方法-简单教学一看就会-以实际例子来说明-优雅草卓伊凡
700 10
大前端之前端开发接口测试工具postman的使用方法-简单get接口请求测试的使用方法-简单教学一看就会-以实际例子来说明-优雅草卓伊凡
|
11月前
|
存储 测试技术 数据库
接口测试工具攻略:轻松掌握测试技巧
在互联网快速发展的今天,软件系统的复杂性不断增加,接口测试工具成为确保系统稳定性的关键。它如同“翻译官”,模拟请求、解析响应、验证结果、测试性能并支持自动化测试,确保不同系统间信息传递的准确性和完整性。通过Apifox等工具,设计和执行测试用例更加便捷高效。接口测试是保障系统稳定运行的第一道防线。
|
11月前
|
Web App开发 JSON 测试技术
API测试工具集合:让接口测试更简单高效
在当今软件开发领域,接口测试工具如Postman、Apifox、Swagger等成为确保API正确性、性能和可靠性的关键。Postman全球闻名但高级功能需付费,Apifox则集成了API文档、调试、Mock与自动化测试,简化工作流并提高团队协作效率,特别适合国内用户。Swagger自动生成文档,YApi开源但功能逐渐落后,Insomnia界面简洁却缺乏团队协作支持,Paw仅限Mac系统。综合来看,Apifox是国内用户的理想选择,提供中文界面和免费高效的功能。
|
12月前
|
监控 测试技术 定位技术
HTTP代理IP响应速度测试方案设计与指标体系
随着数字化发展,网络安全、隐私保护及内容访问自由成为核心需求。HTTP代理因其技术优势成为热门选择。本文介绍HTTP代理IP响应速度测试方案,包括基础性能、稳定性、地理位置、实际应用、安全性测试及监控指标,推荐测试工具,并提供测试结果评估标准。
250 2
|
JSON 移动开发 监控
快速上手|HTTP 接口功能自动化测试
HTTP接口功能测试对于确保Web应用和H5应用的数据正确性至关重要。这类测试主要针对后台HTTP接口,通过构造不同参数输入值并获取JSON格式的输出结果来进行验证。HTTP协议基于TCP连接,包括请求与响应模式。请求由请求行、消息报头和请求正文组成,响应则包含状态行、消息报头及响应正文。常用的请求方法有GET、POST等,而响应状态码如2xx代表成功。测试过程使用Python语言和pycurl模块调用接口,并通过断言机制比对实际与预期结果,确保功能正确性。
621 3
快速上手|HTTP 接口功能自动化测试
|
测试技术 API
软件测试:Postman 工具的使用。开发及测试均需要掌握的测试工具
这篇文章详细介绍了Postman工具的各个模块功能,包括创建请求、集合、环境、自动化测试等,并解释了如何使用Postman进行GET、POST、PUT和DELETE等常见HTTP请求的测试。
|
数据采集 缓存 负载均衡
实测 | 芝麻代理,快代理、熊猫代理、豌豆代理HTTP代理质量测试
哈喽大家,欢迎来到本期知识分享!我们将探讨HTTP代理的质量分析方法,无论新手还是资深用户都能从中受益。首先介绍了HTTP代理的基本概念及其重要性。接着,我们通过两个关键指标——响应时间和可用性来评估代理质量。响应时间可通过`curl`命令测试并计算平均值;可用性则需设置定时任务持续检测,比如使用Python脚本。最后,通过具体案例分析了几家知名代理供应商的表现,其中青果网络在各项指标上表现突出,是进行数据采集等活动的优质选择。记得选择最适合自己的代理服务哦!
实测 | 芝麻代理,快代理、熊猫代理、豌豆代理HTTP代理质量测试
|
测试技术 Python
我们假设要测试一个名为`http://example.com`的网站,并对其进行简单的GET请求性能测试。
我们假设要测试一个名为`http://example.com`的网站,并对其进行简单的GET请求性能测试。
|
Shell Python
`pytest-httpserver`是一个pytest插件,它允许你在测试期间启动一个轻量级的HTTP服务器,并模拟HTTP请求和响应。
`pytest-httpserver`是一个pytest插件,它允许你在测试期间启动一个轻量级的HTTP服务器,并模拟HTTP请求和响应。