Maison  >  Article  >  développement back-end  >  Implémentation de la fonction de test automatisé de l'interface phpunit

Implémentation de la fonction de test automatisé de l'interface phpunit

php中世界最好的语言
php中世界最好的语言original
2018-03-24 13:44:292071parcourir

Cette fois, je vais vous présenter l'implémentation de la fonction de test automatisé de l'interface phpunit. Quelles sont les précautions pour l'implémentation de la fonction de test automatisé de l'interface phpunit. Ce qui suit est un cas pratique, prenons un. regarder.

Je suis entré en contact par hasard avec phpunit en début d'année, un logiciel open source développé en utilisant PHPlangage de programmation C'est aussi un framework de tests unitaires. Si elle est utilisée efficacement, l'interface peut être grandement améliorée. Pas de bêtises, allons droit au but.

1. Installez

dans le répertoire php

pear channel-discover pear; 
pear install phpunit/PHPUnit

2. 🎜>

Créez d'abord un

fichier de configuration stocké dans le dossier lib, puis créez un nouveau fichier 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;
}
Enfin, créez-en un nouveau Le fichier 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); 
}
a été complété pour construire l'environnement de test de cette interface.

3. Rédiger des cas de test

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

S'il s'agit d'une demande de publication

public function testPost(){ 
    $session = $user['retinfo']['sessionid']; 
    $param = array( 
      ,'data' => '111' 
    ); 
    $url = POSTURL.'posturl'; 
    return do_POST($url,$param); 
  }
Je pense que vous maîtrisez la méthode après avoir lu le cas dans cet article. Pour des informations plus intéressantes, veuillez prêter attention aux autres articles connexes sur le site Web chinois de php !

Lecture recommandée :

Tutoriel de mise en œuvre ThinkPHP du processus de paiement WeChat (paiement jsapi) explication détaillée_exemple php

Remboursement WeChat de PHP candidature

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn