测试rest接口的两个工具使用详解(restclient+soapUI)
最近基于webservice开发第三方应用程序,我的第三方API属于rest接口,而开发过程中难免需要测试,于是寻找测试rest接口的工具(自己懒得动手编写代码测试),我现在主要用两种常用的工具:restclient和soupUI
一、使用restclient测试rest接口
RESTClient是一个用于测试RESTful Web services的Java客户端。非常小巧,界面非常简单,看下面这张图你就全明白了!
二、使用soapUI测试rest接口
虽然soapUI看上去好像是测试soap接口的,其实用它来测试rest接口未尝不可。
说明:目前只有test/xml格式成功实现,其他格式待探索。
soapUI由于目前消息体只有test/xml,multipart/form-data,application/xml,这三个选项,而在restclient工具中有几十种消息体格式,所以soapUI的使用还待进一步研究。
目前以合同添加接口为例
地址:http://adsettlement.test.360buy.com/rest/contractinfo.action
消息体类型是:text/xml,content-type=text/xml和charset=utf-8
方法是:POST
消息体:
<ContractInfo> <adsContractState>0</adsContractState> <applyNo>26</applyNo> <contractAmount>550000</contractAmount> <contractBeginTime>2012-08-13T10:55:04.336+08:00</contractBeginTime> <contractEndTime>2013-08-13T10:55:04.336+08:00</contractEndTime> <contractNo>20120026</contractNo> <createTime>2012-08-13T10:55:04.336+08:00</createTime> <departmentNo>0</departmentNo> <depositAmount>120000</depositAmount> <hasDeposit>1</hasDeposit> <isFrameContract>0</isFrameContract> <invoiceType>0</invoiceType> <isHasFrameContract>0</isHasFrameContract> <isUpdatedContract>0</isUpdatedContract> <plan_id>0000026</plan_id> <plan_name>pk</plan_name> <projectNo>20120026</projectNo> <salesmanName>赵玉梅</salesmanName> <settlementContractState>1</settlementContractState> <AdsInfo> <parta_no>7</parta_no> <parta_name>zymtest7</parta_name> <partb_no>14</partb_no> <partb_name>京东商城</partb_name> <ads_amount>280000</ads_amount> <has_deposit>1</has_deposit> <deposit_amount>90000</deposit_amount> <pay_account_deadline>2012-10-30</pay_account_deadline> <last_charge_time>2012-10-30</last_charge_time> </AdsInfo> <AdsInfo> <parta_no>8</parta_no> <parta_name>zymtest8</parta_name> <partb_no>14</partb_no> <partb_name>京东商城</partb_name> <ads_amount>220000</ads_amount> <has_deposit>1</has_deposit> <deposit_amount>40000</deposit_amount> <pay_account_deadline>2012-09-30</pay_account_deadline> <last_charge_time>2012-9-30</last_charge_time> </AdsInfo> </ContractInfo>
其中<adsinfo>可以增加,增加整个list即可
操作步骤
1、打开soapUI,创建工程
如图,右键,点击New soapUI Project
2、弹出创建工程窗口
在project name中输入工程名称,如Test,rest 接口勾选Add REST Service(这个必须勾选),点击“OK”按钮
3、弹出创建新rest服务页面,输入接口的域名地址,勾选opens dialog to create a REST Resource,点击“OK”按钮
4、弹出New REST Resource窗口,输入名称输入resource URL,点击“OK”按钮
5、弹出 New REST Method窗口,输入方法名称,选择API方法,点击“OK”按钮
6、创建请求成功,request请求页面如下
7、选择消息体格式,输入认证密码,输入消息体:
8、执行,查看执行结果:
9、在outline界面可以修改参数:
最后,给大家推荐个网址,我今天下午才看到的,希望对你们有用,嘿嘿
http://126.am/oCwwe3 (复制网址到浏览器中打开)
gcov 简述
代码覆盖率是单元测试的一个指标,通常覆盖率越高,单元测试就做得更完备。(然而,覆盖率是不是和软件质量成正比关系呢?)gcov是GNU工具链中的一个重要的工具,虽然gcov是覆盖率很好的工具,但是gcov的更重要的应用是性能的调优。gcov通过监视程序的执行,从而确定某行代码有没有执行,执行了多少次。gcov的报告是基于文本的格式的,看起来是比较难看点。但是,有个叫lcov的工具,将gcov的报告格式转换为html的直观形式,后面介绍。
gcov使用:
如有以下代码:
1: #include <stdio.h>
2:
3: void bubbleSort( int list[], int size )
4: {
5: int i, j, temp, swap = 1;
6:
7: while (swap) {
8:
9: swap = 0;
10:
11: for ( i = (size-1) ; i >= 0 ; i-- ) {
12:
13: for ( j = 1 ; j <= i ; j++ ) {
14:
15: if ( list[j-1] > list[j] ) {
16:
17: temp = list[j-1];
18: list[j-1] = list[j];
19: list[j] = temp;
20: swap = 1;
21:
22: }
23:
24: }
25:
26: }
27:
28: }
29:
30:
31: }
32:
33: int main()
34: {
35: int theList[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
36: int i;
37:
38: /* Invoke the bubble sort algorithm */
39: bubbleSort( theList, 10 );
40:
41: /* Print out the final list */
42: for (i = 0 ; i < 10 ; i++) {
43: printf("%d\n", theList[i]);
44: }
45: if(i == 0){
46: printf("i = 0\n");
47: }else{
48: printf("i != 0\n");
49: }
50:
51: }
1. 编译程序是增加 -ftest-coverage -fprofile-arcs 选项。
[heidong@HEIDONGVM gcov]$ gcc -o bbsort bbsort.c -ftest-coverage -fprofile-arcs
生成.gcno文件。
2. 执行程序,将生成.gcda文件,用gcov程序检查相应的源代码文件,将生成结果文件。
[heidong@HEIDONGVM gcov]$ ./bbsort
1
2
3
4
5
6
7
8
9
10
i != 0
[heidong@HEIDONGVM gcov]$ gcov bbsort.c
File‘bbsort.c’
已执行的行数:95.24% (共 21 行)
bbsort.c:正在创建‘bbsort.c.gcov
3. 检查相应的结果文件
[heidong@HEIDONGVM gcov]$ cat bbsort.c.gcov
-: 0:Source:bbsort.c
-: 0:Graph:bbsort.gcno
-: 0:Data:bbsort.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:
1: 3:void bubbleSort( int list[], int size )
-: 4:{
1: 5: int i, j, temp, swap = 1;
-: 6:
4: 7: while (swap) {
-: 8:
2: 9: swap = 0;
-: 10:
22: 11: for ( i = (size-1) ; i >= 0 ; i-- ) {
-: 12:
110: 13: for ( j = 1 ; j <= i ; j++ ) {
-: 14:
90: 15: if ( list[j-1] > list[j] ) {
-: 16:
45: 17: temp = list[j-1];
45: 18: list[j-1] = list[j];
45: 19: list[j] = temp;
45: 20: swap = 1;
-: 21:
-: 22: }
-: 23:
-: 24: }
-: 25:
-: 26: }
-: 27:
-: 28: }
-: 29:
-: 30:
1: 31:}
-: 32:
1: 33:int main()
-: 34:{
1: 35: int theList[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
-: 36: int i;
-: 37:
-: 38: /* Invoke the bubble sort algorithm */
1: 39: bubbleSort( theList, 10 );
-: 40:
-: 41: /* Print out the final list */
11: 42: for (i = 0 ; i < 10 ; i++) {
10: 43: printf("%d\n", theList[i]);
-: 44: }
1: 45: if(i == 0){
#####: 46: printf("i = 0\n");
-: 47: }else{
1: 48: printf("i != 0\n");
-: 49: }
-: 50:
1: 51:}
-: 52:
[heidong@HEIDONGVM gcov]$
可以看到某行执行了多少次,哪些行没有执行过(####标示)。gcov 还可以检查其他很多的信息,如分支,函数等,详细参考gcov的帮助文档,并测试之。
对于文本格式,相信很多人的觉得不是很直观,于是便有了lcov这个工具,它可以算是gcov的前端工具,这样生成html文件,可以很直观的看到代码覆盖情况。
lcov不是标准的unix/linux工具,需要下载,地址是:ltp.sourceforge.net/coverage/lcov.php 注意要FQ才可以访问。
使用方式:
1. 执行完gcov的步骤后,执行下列命令:
[heidong@HEIDONGVM gcov]$ lcov --capture --directory ./ --output-file bbsort.info
Capturing coverage data from ./
Found gcov version: 4.4.6
Scanning ./ for .gcda files ...
Found 1 data files in ./
Processing bbsort.gcda
Finished .info-file creation
2. 生成html文件:
[heidong@HEIDONGVM gcov]$ genhtml bbsort.info --output-directory ./lcov/
Reading data file bbsort.info
Found 1 entries.
Found common filename prefix "/home/heidong/tmp"
Writing .css and .png files.
Generating output.
Processing file gcov/bbsort.c
Writing directory view page.
Overall coverage rate:
lines......: 95.2% (20 of 21 lines)
functions..: 100.0% (2 of 2 functions
看下生成的html文件:
完毕。



浙公网安备 33010602011771号