>  기사  >  백엔드 개발  >  phpunit 인터페이스의 자동화된 테스트 기능 구현

phpunit 인터페이스의 자동화된 테스트 기능 구현

php中世界最好的语言
php中世界最好的语言원래의
2018-03-24 13:44:292071검색

이번에는 phpunit 인터페이스 자동화 테스트 기능 구현에 대해 알려드리겠습니다. phpunit 인터페이스 자동화 테스트 기능 구현 시 주의사항은 무엇인지 살펴보겠습니다.

연초에 우연히 접한 phpunit은 PHP프로그래밍 언어를 사용하여 개발된 오픈소스 소프트웨어이기도 합니다. 효과적으로 사용하면 효율성을 크게 높일 수 있는 단위 테스트프레임워크이기도 합니다. 인터페이스 순회. 별로 말도 안 되는 얘기는 아니고 바로 본론으로 들어가겠습니다.

1. php 디렉토리

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, &#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;
}

드디어 새로운 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 . &#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);
  }

게시물 요청이라면

public function testPost(){ 
    $session = $user['retinfo']['sessionid']; 
    $param = array( 
      ,'data' => '111' 
    ); 
    $url = POSTURL.'posturl'; 
    return do_POST($url,$param); 
  }

이 글의 케이스를 읽어보신 후 방법을 익히셨을 거라 믿습니다. PHP 중국어 웹사이트에 관련 기사가 있습니다!

추천 도서:

WeChat 결제(jsapi 결제) 프로세스 튜토리얼의 ThinkPHP 구현 상세 설명_php 예시

PHP의 WeChat 환불 애플리케이션

위 내용은 phpunit 인터페이스의 자동화된 테스트 기능 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.