使用phpunit進(jìn)行接口自動(dòng)化測(cè)試
年初一個(gè)偶然的機(jī)會(huì)接觸到了phpunit,一個(gè)用PHP編程語(yǔ)言開發(fā)的開源軟件,也是一個(gè)單元測(cè)試框架,有效利用的話可以大大提高接口遍歷的效率。廢話不多說(shuō),直接干貨。
1.安裝
在php的目錄下
pear channel-discover pear; pear install phpunit/PHPUnit
2.配置
首先新建一個(gè)lib文件夾存放的配置文件,然后再新建一個(gè)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; }
最后新建一個(gè)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); }
到此接口測(cè)試環(huán)境搭建完成。
3.編寫測(cè)試用例
<?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請(qǐng)求
public function testPost(){ $session = $user['retinfo']['sessionid']; $param = array( ,'data' => '111' ); $url = POSTURL.'posturl'; return do_POST($url,$param); }
以上這篇使用phpunit進(jìn)行接口自動(dòng)化測(cè)試就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Windows下安裝PHP單元測(cè)試環(huán)境PHPUnit圖文教程
- PHP單元測(cè)試?yán)?PHPUNIT深入用法(三)
- PHP單元測(cè)試?yán)?PHPUNIT初探
- PHP單元測(cè)試?yán)?PHPUNIT深入用法(二)
- php單元測(cè)試phpunit入門實(shí)例教程
- PHP單元測(cè)試PHPUnit簡(jiǎn)單用法示例
- PHPUnit PHP測(cè)試框架安裝方法
- 詳解Yaf框架PHPUnit集成測(cè)試方法
- PHPUnit測(cè)試私有屬性和方法功能示例
- PHP測(cè)試框架PHPUnit組織測(cè)試操作示例
- PHP單元測(cè)試框架PHPUnit用法詳解
相關(guān)文章
老生常談ThinkPHP中的行為擴(kuò)展和插件(推薦)
下面小編就為大家?guī)?lái)一篇老生常談ThinkPHP中的行為擴(kuò)展和插件(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05關(guān)于laravel 日志寫入失敗問(wèn)題匯總
今天小編就為大家分享一篇關(guān)于laravel 日志寫入失敗問(wèn)題匯總,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10php網(wǎng)上商城購(gòu)物車設(shè)計(jì)代碼分享
我們要做的是一個(gè)可以包含促銷活動(dòng)的購(gòu)物車,所以比較其他簡(jiǎn)單的購(gòu)物車,會(huì)稍微復(fù)雜一點(diǎn)。(用的是PHP的zend framework框架)2012-02-02php arsort 數(shù)組降序排序詳細(xì)介紹
php arsort函數(shù)用于將數(shù)組中的元素按照降序進(jìn)行排序,如果排序成功則返回true,否則返回false,本文章向大家講解arsort函數(shù)的基本語(yǔ)法及使用實(shí)例,需要的朋友可以參考下2016-11-11微信公眾號(hào)判斷用戶是否已關(guān)注php代碼解析
這篇文章主要大家詳細(xì)解析了微信公眾號(hào)判斷用戶是否已關(guān)注php代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Yii2.0框架behaviors方法使用實(shí)例分析
這篇文章主要介紹了Yii2.0框架behaviors方法使用,結(jié)合實(shí)例形式分析了yii2.0框架控制器 behaviors 過(guò)濾數(shù)據(jù)相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-09-09一個(gè)自定義位數(shù)的php多用戶計(jì)數(shù)器代碼
一個(gè)自定義位數(shù)的php多用戶計(jì)數(shù)器代碼...2007-03-03Laravel 5.4.36中session沒(méi)有保存成功問(wèn)題的解決
這篇文章主要給大家介紹了關(guān)于Laravel 5.4.36中session沒(méi)有保存成功問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02