一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - 使用phpunit進行接口自動化測試

使用phpunit進行接口自動化測試

2019-10-16 11:12愛吃燒麥的管 PHP教程

下面小編就為大家分享一篇使用phpunit進行接口自動化測試,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

年初一個偶然的機會接觸到了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進行接口自動化測試就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲 欧美 国产 视频二区 | 亚洲国产婷婷俺也色综合 | 人与动人物性行为zozo共患病 | 日本免费一区二区三区 | 亚洲 欧美 在线观看 | ts人妖国产一区 | 久久成人永久免费播放 | 成年男女免费视频网站 | 欧美xxxxxbb| 1024毛片 | 国产资源免费观看 | 成人福利在线观看 | 亚洲青草视频 | 波多野结衣xxxx性精品 | 国产亚洲综合成人91精品 | 日本高清不卡一区久久精品 | 欧美日韩精品一区二区三区视频播放 | 丁香网五月天 | 国内精品视频九九九九 | 亚洲网站在线 | 92国产福利久久青青草原 | 四神集团1涨奶是第几章 | 国产精品一区三区 | 黄色a | 日本中文字幕不卡在线一区二区 | 午夜剧场1000 | 国产成人盗摄精品 | 狠狠香蕉 | 单亲乱l仑在线观看免费观看 | 久久久久久免费观看 | a v在线男人的天堂观看免费 | 超91在线 | free哆拍拍免费永久视频 | 国产午夜大片 | 欧美日韩国产超高清免费看片 | 99久久无色码中文字幕 | 香蕉久久高清国产精品免费 | 色中色导航 | 高清男的插曲女的 欢迎你老狼 | 风间由美被义子中文字幕 | fc2免费人成为视频 eeuss18影院www国产 |