最近在做的项目,需要为其他产品提供一些接口,如果由QA测试的话,就还需要一个图形化的页面,而直接看接口的返回值,都是json字符串,就感觉比较麻烦和不靠谱。
所以,使用了PHP自带的run-tests.php,实现一些自动化测试的功能。刚开始玩这个,理解较浅。仅给一个具体的例子。
比如,针对某个接口http://interface.a.com/ri.php?act=test&id=23&cnt=1,返回值是:
{“Result”:1,”Msg”:[{“ID”:12345,”BlogId”:890,”Title”:”\u4e00\u5f00\u59cb\u7684\u672a\u6765\u3002\uff09″,”Description”:”\u4e00\u5929\u6bd4\u4e00\u5929\u7737\u604b\u3002\uff09 ……- -\u3001\u7761\u7720\u3002 . \u81ea\u52a9\u9910\u2014\u2014\u5168\u5e26\u58f3\u7684\u98df\u7269\u3002 \u72fc\u7c4d\u4e00\u7247\u3002 \u2014\u2014\u2014\u2014\u751f\u6d3b…”,”DateAdded”:”Sep 14 2010 10:38AM”}]}
其中Result是用来标示接口处理是否成功的,如果成功则具体信息是在Msg里。在这种情况下,Msg是一个数组,数组中每个元素可以decode为一个stdClass的对象。利用反射,我们可以取得该对象的属性名称。(其实decode为数组,直接获取其keys也可以)
那么如果直接写代码,为:
$jsonStr = get_http_result(“http://interface.a.com/ri.php?act=test&id=23&cnt=1″);
$obj = json_decode($jsonStr);
$reflect = new ReflectionObject($obj->Msg[0]);
$props = $reflect->getProperties();
sort($props); # 排序,以保证输出唯一
foreach ($props as $prop) {
print $prop->getName() . “\n”;
}
function get_http_result($url) {
assert(strlen($url));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “$url”);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if( $info[‘http_code’] != 200 )
return ”;
return $result;
}
在这里,我只想进行简单的测试,确保接口返回的keys是一致的就可以了,所以以上代码满足我的要求。那么写入到自动化测试脚本为001.phpt:
–TEST–
interface test
–SKIPIF–
–FILE–
<?php
$jsonStr = get_http_result(“http://interface.a.com/ri.php?act=test&id=23&cnt=1″);
$obj = json_decode($jsonStr);
$reflect = new ReflectionObject($obj->Msg[0]);
$props = $reflect->getProperties();
sort($props); # 排序,以保证输出唯一
foreach ($props as $prop) {
print $prop->getName() . “\n”;
}
function get_http_result($url) {
assert(strlen($url));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “$url”);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if( $info[‘http_code’] != 200 )
return ”;
return $result;
}
?>
–EXPECTF–
BlogId
DateAdded
Description
ID
Title
在保证当前目录可写的情况下,从php源代码复制run-tests.php到当前目录,并设置export TEST_PHP_EXECUTABLE=”/path/to/php/bin/php”,执行:
php run-tests.php 001.phpt,看到的结果应该类似:
=====================================================================
CWD : /curwork/web/autotest
PHP : /usr/local/php/bin/php
PHP_SAPI : cli
PHP_VERSION : 5.2.5
ZEND_VERSION: 2.2.0
PHP_OS : Linux
INI actual : /etc/php.ini
More .INIs :
Extra dirs :
=====================================================================
Running selected tests.
PASS rec fashion/feeling/travel blogs [001.phpt]
=====================================================================
Number of tests : 1 1
Tests skipped : 0 ( 0.0%) ——–
Tests warned : 0 ( 0.0%) ( 0.0%)
Tests failed : 0 ( 0.0%) ( 0.0%)
Tests passed : 1 (100.0%) (100.0%)
———————————————————————
Time taken : 0 seconds
=====================================================================
IBM的文档很清楚:http://www.ibm.com/developerworks/cn/opensource/os-cn-php-autotest/index.html