年初一個偶然的機會接觸到了phpunit,一個用PHP編程語言開發(fā)的開源軟件,也是一個單元測試框架,有效利用的話可以大大提高接口遍歷的效率。廢話不多說,直接干貨。
1.安裝
在php的目錄下
1 2 |
|
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, '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);
}
到此接口測試環(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);
}
總結:
感謝每一個認真閱讀我文章的人?。。?/strong>
作為一位過來人也是希望大家少走一些彎路,如果你不想再體驗一次學習時找不到資料,沒人解答問題,堅持幾天便放棄的感受的話,在這里我給大家分享一些自動化測試的學習資源,希望能給你前進的路上帶來幫助。
這份文檔,對于想從事【軟件測試】的朋友來說應該是最全面最完整的備戰(zhàn)倉庫,這個倉庫也陪伴我走過了最艱難的路程,希望也能幫助到你!文章來源:http://www.zghlxwxcb.cn/news/detail-807951.html
以上均可以分享,只需要你搜索vx公眾號:程序員雨果,即可免費領取文章來源地址http://www.zghlxwxcb.cn/news/detail-807951.html
到了這里,關于使用phpunit進行接口自動化測試的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!