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

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

1.安装

在php的目录下

1

2

pear channel-discover pear;

pear install phpunit/PHPUnit

2.配置

首先新建一个lib文件夹存放的配置文件,然后再新建一个transfer.php的文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

<?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文件

1

2

3

4

5

6

7

8

<?php

require_once("transfer.php");

define("PREFIX", "http://xxx");

define("HTTPSPREFIX", "https://xxx");

  

function build_get_param($param) {

    return http_build_query($param);

}

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

3.编写测试用例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

<?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请求

1

2

3

4

5

6

7

8

public function testPost(){

    $session = $user['retinfo']['sessionid'];

    $param = array(

      ,'data' => '111'

    );

    $url = POSTURL.'posturl';

    return do_POST($url,$param);

  }

​现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值