这篇文章将为大家详细讲解有关使用phpunit怎么实现一个接口自动化测试功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1.安装
在php的目录下
pear channel-discover pear; pear install phpunit/PHPUnit
2.配置
首先新建一个lib文件夹存放的配置文件,然后再新建一个transfer.php的文件
<?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, $extraheader = array()){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $extraheader); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回: //curl_setopt($ch, CURLOPT_VERBOSE, true); $output = curl_exec($ch) ; curl_close($ch); return $output; } function do_Put($url, $fields, $extraheader = array()){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ) ; curl_setopt($ch, CURLOPT_POST, true) ; curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields ); curl_setopt($ch, CURLOPT_HTTPHEADER, $extraheader); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回 //curl_setopt($ch, CURLOPT_ENCODING, ''); $output = curl_exec($ch); curl_close($ch); return $output; } function do_Delete($url, $fields, $extraheader = array()){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ) ; curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_HTTPHEADER, $extraheader); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回 //curl_setopt($ch, CURLOPT_ENCODING, ''); $output = curl_exec($ch); curl_close($ch); return $output; }
最后新建一个basetest.php文件
<?php require_once("transfer.php"); define("PREFIX", "http://xxx"); define("HTTPSPREFIX", "https://xxx"); function build_get_param($param) { return http_build_query($param); }
到此接口测试环境搭建完成。
3.编写测试用例
<?php $basedir = dirname(__FILE__); require_once($basedir . '/lib/basetestdev.php'); define("PHONE", "xxx"); define("PWD", "xxx"); define("POSTURL","xxx"); class TestAPI extends PHPUnit_Framework_TestCase { private function call_http($path, $param, $expect = 'ok') { $_param = build_get_param($param); $url = PREFIX . "$path?" . $_param; $buf = do_Get($url); $obj = json_decode($buf, True); $this->assertEquals($obj['retval'], $expect); return $obj; } private function call_https($path, $param, $expect = 'ok') { $_param = build_get_param($param); $url = HTTPSPREFIX . "$path?" . $_param; $buf = do_Get($url); $obj = json_decode($buf, True); $this->assertEquals($obj['retval'], $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请求
public function testPost(){ $session = $user['retinfo']['sessionid']; $param = array( ,'data' => '111' ); $url = POSTURL.'posturl'; return do_POST($url,$param); }
关于使用phpunit怎么实现一个接口自动化测试功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。