phpunit介面自動化測試功能的實現

來源:互聯網
上載者:User
這次給大家帶來phpunit介面自動化測試功能的實現,phpunit介面自動化測試功能實現的注意事項有哪些,下面就是實戰案例,一起來看一下。

年初一個偶然的機會接觸到了phpunit,一個用PHP程式設計語言開發的開源軟體,也是一個單元測試架構,有效利用的話可以大大提高介面遍曆的效率。廢話不多說,直接乾貨。

1.安裝

在php的目錄下

pear channel-discover pear; pear install phpunit/PHPUnit

2.配置

首先建立一個lib檔案夾存放的設定檔,然後再建立一個transfer.php的檔案

<?phpfunction 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);   }

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

ThinkPHP實現支付(jsapi支付)流程教程詳解_php執行個體

PHP的退款申請

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.