微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用phpunit进行接口自动化测试

年初一个偶然的机会接触到了PHPunit,一个PHP编程语言开发的开源软件,也是一个单元测试框架,有效利用的话可以大大提高接口遍历的效率。废话不多说,直接干货。

1.安装

PHP的目录下

rush:xhtml;"> pear channel-discover pear; pear install PHPunit/PHPUnit

2.配置

首先新建一个lib文件夹存放的配置文件,然后再新建一个transfer.PHP文件

rush:xhtml;"> PHP function do_Post($url,$fields,$extraheader = array()){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_POSTFIELDS,$fields ); curl_setopt($ch,CURLOPT_HTTPHEADER,$extraheader); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); // 获取数据返回 $output = curl_exec($ch); curl_close($ch); return $output; } function do_Get($url,true); // 获取数据返回: //curl_setopt($ch,CURLOPT_VERBOSE,true); $output = curl_exec($ch) ; curl_close($ch); return $output; } function do_Put($url,$url ) ; curl_setopt($ch,true) ; curl_setopt($ch,CURLOPT_CUSTomrEQUEST,'PUT'); curl_setopt($ch,true); // 获取数据返回 //curl_setopt($ch,CURLOPT_ENCODING,''); $output = curl_exec($ch); curl_close($ch); return $output; } function do_Delete($url,'DELETE'); curl_setopt($ch,$fields); curl_setopt($ch,''); $output = curl_exec($ch); curl_close($ch); return $output; }

最后新建一个basetest.PHP文件

rush:xhtml;"> PHP require_once("transfer.PHP"); define("PREFIX","http://xxx"); define("HTTPSPREFIX","https://xxx");

function build_get_param($param) {
return http_build_query($param);
}

到此接口测试环境搭建完成。

3.编写测试用例

rush:xhtml;"> assertEquals($obj['retval'],$expect); return $obj; } private function call_https($path,$expect = 'ok') { $_param = build_get_param($param); $url = HTTPSPREFIX . "$path?" . $_param; $buf = do_Get($url); $obj = json_decode($buf,$expect); return $obj; } public function testLogin(){ $param = array( 'type' => 'phone','token' => PHONE,'password' => PWD ); $url = 'login'; return $this->call_http($url,$param); } /** * @depends testLogin */ public function testInfo(array $user){ $session = $user['retinfo']['session']; $param = array( 'session' => $session ); $url ='info'; return $this->call_http($url,$param); }

如果为post请求

'111' ); $url = POSTURL.'posturl'; return do_POST($url,$param); }

以上这篇使用PHPunit进行接口自动化测试就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐