phpunit进行接口自动化步骤详解

php中世界最好的语言
Lepaskan: 2023-03-26 12:04:01
asal
1591 orang telah melayarinya

这次给大家带来phpunit进行接口自动化步骤详解,phpunit进行接口自动化的注意事项有哪些,下面就是实战案例,一起来看一下。

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

1.安装

在php的目录下

pear channel-discover pear; 
pear install phpunit/PHPUnit
Salin selepas log masuk

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, &#39;PUT&#39;);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
  curl_setopt($ch, CURLOPT_HTTPHEADER, $extraheader);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
  //curl_setopt($ch, CURLOPT_ENCODING, &#39;&#39;);
  $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, &#39;DELETE&#39;);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $extraheader);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
  //curl_setopt($ch, CURLOPT_ENCODING, &#39;&#39;);
  $output = curl_exec($ch);
  curl_close($ch);
  return $output;
}
Salin selepas log masuk

最后新建一个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); 
}
Salin selepas log masuk

到此接口测试环境搭建完成。

3.编写测试用例

<?php
$basedir = dirname(FILE);
require_once($basedir . &#39;/lib/basetestdev.php&#39;);
define("PHONE", "xxx");
define("PWD", "xxx");
define("POSTURL","xxx");
class TestAPI extends PHPUnit_Framework_TestCase {
    private function call_http($path, $param, $expect = &#39;ok&#39;) {
        $_param = build_get_param($param);
        $url = PREFIX . "$path?" . $_param;
        $buf = do_Get($url);
        $obj = json_decode($buf, True);
        $this->assertEquals($obj[&#39;retval&#39;], $expect);
        return $obj;
    }
    private function call_https($path, $param, $expect = &#39;ok&#39;) {
        $_param = build_get_param($param);
        $url = HTTPSPREFIX . "$path?" . $_param;
        $buf = do_Get($url);
        $obj = json_decode($buf, True);
        $this->assertEquals($obj[&#39;retval&#39;], $expect);
        return $obj;
    }
  public function testLogin(){
    $param = array(
      &#39;type&#39; => &#39;phone&#39;
      ,&#39;token&#39; => PHONE
      ,&#39;password&#39; => PWD
    );
    $url = &#39;login&#39;;
    return $this->call_http($url, $param);
  }
  /**
   * @depends testLogin
   */
  public function testInfo(array $user){
    $session = $user[&#39;retinfo&#39;][&#39;session&#39;];
    $param = array(
      &#39;session&#39; => $session
    );
    $url =&#39;info&#39;;
    return $this->call_http($url, $param);
  }
Salin selepas log masuk

如果为post请求

public function testPost(){ 
    $session = $user[&#39;retinfo&#39;][&#39;sessionid&#39;]; 
    $param = array( 
      ,&#39;data&#39; => &#39;111&#39; 
    ); 
    $url = POSTURL.&#39;posturl&#39;; 
    return do_POST($url,$param); 
  }
Salin selepas log masuk

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

ThinkPHP框架用户信息查询更新与删除步骤详解

ThinkPHP连接数据库操作案列分析

Atas ialah kandungan terperinci phpunit进行接口自动化步骤详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!