您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

phpunit接口自动化测试功能的实现

2024/2/28 8:20:42发布15次查看
这次给大家带来phpunit接口自动化测试功能的实现,phpunit接口自动化测试功能实现的注意事项有哪些,下面就是实战案例,一起来看一下。
年初一个偶然的机会接触到了phpunit,一个用php编程语言开发的开源软件,也是一个单元测试框架,有效利用的话可以大大提高接口遍历的效率。废话不多说,直接干货。
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);    }
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
thinkphp实现微信支付(jsapi支付)流程教程详解_php实例
php的微信退款申请
以上就是phpunit接口自动化测试功能的实现的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product