Home  >  Article  >  Backend Development  >  Detailed explanation of interface automation steps in phpunit

Detailed explanation of interface automation steps in phpunit

php中世界最好的语言
php中世界最好的语言Original
2018-05-17 15:08:251592browse

This time I will bring you a detailed explanation of the steps for phpunit to implement interface automation. What are the precautions for phpunit to implement interface automation? . Here are actual cases, let’s take a look.

I came into contact with phpunit by chance at the beginning of the year, an open source software developed with PHPprogramming language, and also a unit testing framework. If used effectively, the interface can be greatly improved. Traversal efficiency. Not much nonsense, let’s get straight to the point.

1.Install

In the directory of php

pear channel-discover pear; 
pear install phpunit/PHPUnit

2.Configuration

First create a configuration file stored in the lib folder, then create a new transfer.php file

<?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;
}

Finally create a basetest.php file

<?php 
require_once("transfer.php"); 
define("PREFIX", "http://xxx"); 
define("HTTPSPREFIX", "https://xxx"); 
 
function build_get_param($param) { 
    return http_build_query($param); 
}

The interface test environment is now set up.

3. Write test cases

<?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);
  }

If it is a post request

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); 
  }

I believe you have read it You have mastered the method in the case of this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of user information query, update and delete steps in the ThinkPHP framework

ThinkPHP connection database operation case analysis

The above is the detailed content of Detailed explanation of interface automation steps in phpunit. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn