首頁 >後端開發 >php教程 >phpunit進行介面自動化步驟詳解

phpunit進行介面自動化步驟詳解

php中世界最好的语言
php中世界最好的语言原創
2018-05-17 15:08:251635瀏覽

這次帶給大家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