佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 使用phpunit進(jìn)行接口自動化測試

使用phpunit進(jìn)行接口自動化測試

熱門標(biāo)簽:阿里云 服務(wù)器配置 Mysql連接數(shù)設(shè)置 電子圍欄 科大訊飛語音識別系統(tǒng) Linux服務(wù)器 銀行業(yè)務(wù) 團(tuán)購網(wǎng)站

年初一個偶然的機(jī)會接觸到了phpunit,一個用PHP編程語言開發(fā)的開源軟件,也是一個單元測試框架,有效利用的話可以大大提高接口遍歷的效率。廢話不多說,直接干貨。

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); // 獲取數(shù)據(jù)返回
  $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); // 獲取數(shù)據(jù)返回:
  //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); // 獲取數(shù)據(jù)返回
  //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); // 獲取數(shù)據(jù)返回
  //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); 
} 

到此接口測試環(huán)境搭建完成。

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進(jìn)行接口自動化測試就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Windows下安裝PHP單元測試環(huán)境PHPUnit圖文教程
  • PHP單元測試?yán)?PHPUNIT深入用法(三)
  • PHP單元測試?yán)?PHPUNIT初探
  • PHP單元測試?yán)?PHPUNIT深入用法(二)
  • php單元測試phpunit入門實(shí)例教程
  • PHP單元測試PHPUnit簡單用法示例
  • PHPUnit PHP測試框架安裝方法
  • 詳解Yaf框架PHPUnit集成測試方法
  • PHPUnit測試私有屬性和方法功能示例
  • PHP測試框架PHPUnit組織測試操作示例
  • PHP單元測試框架PHPUnit用法詳解

標(biāo)簽:廣元 萍鄉(xiāng) 衢州 衡水 棗莊 蚌埠 大理 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用phpunit進(jìn)行接口自動化測試》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    灌云县| 北流市| 东源县| 新昌县| 南召县| 邵阳县| 苏州市| 五大连池市| 沂源县| 河池市| 重庆市| 安远县| 湾仔区| 米易县| 多伦县| 新密市| 上林县| 平武县| 军事| 宁夏| 肥西县| 县级市| 即墨市| 扬中市| 宁城县| 东山县| 常山县| 江城| 蓝田县| 开远市| 云霄县| 英超| 莱芜市| 旺苍县| 高雄市| 张家港市| 汶川县| 蓬溪县| 大足县| 轮台县| 池州市|