使用phpunit进行接口自动化测试
发布:smiling 来源: PHP粉丝网 添加日期:2021-09-05 16:45:25 浏览: 评论:0
下面小编就为大家分享一篇使用phpunit进行接口自动化测试,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
年初一个偶然的机会接触到了phpunit,一个用PHP编程语言开发的开源软件,也是一个单元测试框架,有效利用的话可以大大提高接口遍历的效率,废话不多说,直接干货。
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); // 获取数据返回
- $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);
- }
Tags: phpunit php接口测试
相关文章
- ·PHPUnit安装及使用示例(2021-04-19)
- ·PHP单元测试框架PHPUnit用法详解(2021-11-05)
- ·PHP使用phpunit进行单元测试示例(2021-12-21)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)