使用phpunit进行接口自动化测试

年初一个偶然的机会接触到了phpunit,一个用PHP编程语言开发的开源软件,也是一个单元测试框架,有效利用的话可以大大提高接口遍历的效率。废话不多说,直接干货。

1.安装

在php的目录下

|-----|--------------------------------------------------------------|
| 1 2 | 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);

  }
总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

这份文档,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

以上均可以分享,只需要你搜索vx公众号:程序员雨果,即可免费领取

相关推荐
byte轻骑兵31 分钟前
【Bluedroid】蓝牙启动之 SMP_Init 源码解析
android·c++·smp·bluedroid
每次的天空1 小时前
Android第十四次面试总结
android·面试·职场和发展
androidwork2 小时前
Android 布局优化:掌握 <include> 与 <merge> 的实战技巧
android
Jiaberrr2 小时前
uniapp 安卓 APP 后台持续运行(保活)的尝试办法
android·前端·javascript·uni-app·app·保活
gfgfgg0012 小时前
谷歌地图手机版(Google maps)v11.152.0100安卓版 - 前端工具导航
android·智能手机
清霜之辰2 小时前
安卓Compose实现鱼骨加载中效果
android
Shujie_L2 小时前
【Android基础回顾】五:AMS(Activity Manager Service)
android
我又来搬代码了2 小时前
【Android】Android Studio项目代码异常错乱问题处理(2020.3版本)
android·ide·android studio
雨白3 小时前
Fragment 最佳实践:兼容手机和平板的简易新闻应用
android
洞见前行3 小时前
Java反射机制详细指南
android