首页 >后端开发 >php教程 >phpunit进行接口自动化步骤详解

phpunit进行接口自动化步骤详解

php中世界最好的语言
php中世界最好的语言原创
2018-05-17 15:08:251613浏览

这次给大家带来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, &#39;PUT&#39;);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
  curl_setopt($ch, CURLOPT_HTTPHEADER, $extraheader);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
  //curl_setopt($ch, CURLOPT_ENCODING, &#39;&#39;);
  $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, &#39;DELETE&#39;);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $extraheader);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
  //curl_setopt($ch, CURLOPT_ENCODING, &#39;&#39;);
  $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 . &#39;/lib/basetestdev.php&#39;);
define("PHONE", "xxx");
define("PWD", "xxx");
define("POSTURL","xxx");
class TestAPI extends PHPUnit_Framework_TestCase {
    private function call_http($path, $param, $expect = &#39;ok&#39;) {
        $_param = build_get_param($param);
        $url = PREFIX . "$path?" . $_param;
        $buf = do_Get($url);
        $obj = json_decode($buf, True);
        $this->assertEquals($obj[&#39;retval&#39;], $expect);
        return $obj;
    }
    private function call_https($path, $param, $expect = &#39;ok&#39;) {
        $_param = build_get_param($param);
        $url = HTTPSPREFIX . "$path?" . $_param;
        $buf = do_Get($url);
        $obj = json_decode($buf, True);
        $this->assertEquals($obj[&#39;retval&#39;], $expect);
        return $obj;
    }
  public function testLogin(){
    $param = array(
      &#39;type&#39; => &#39;phone&#39;
      ,&#39;token&#39; => PHONE
      ,&#39;password&#39; => PWD
    );
    $url = &#39;login&#39;;
    return $this->call_http($url, $param);
  }
  /**
   * @depends testLogin
   */
  public function testInfo(array $user){
    $session = $user[&#39;retinfo&#39;][&#39;session&#39;];
    $param = array(
      &#39;session&#39; => $session
    );
    $url =&#39;info&#39;;
    return $this->call_http($url, $param);
  }

如果为post请求

public function testPost(){ 
    $session = $user[&#39;retinfo&#39;][&#39;sessionid&#39;]; 
    $param = array( 
      ,&#39;data&#39; => &#39;111&#39; 
    ); 
    $url = POSTURL.&#39;posturl&#39;; 
    return do_POST($url,$param); 
  }

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

ThinkPHP框架用户信息查询更新与删除步骤详解

ThinkPHP连接数据库操作案列分析

以上是phpunit进行接口自动化步骤详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn