ホームページ  >  記事  >  バックエンド開発  >  phpunitインターフェースの自動テスト機能の実装

phpunitインターフェースの自動テスト機能の実装

php中世界最好的语言
php中世界最好的语言オリジナル
2018-03-24 13:44:292071ブラウズ

今回はphpunitインターフェース自動テスト機能の実装についてお届けします。 phpunitインターフェース自動テスト機能を実装する際の注意点について、実際の事例を見てみましょう。

私は、PHPプログラミング言語を使って開発されたオープンソースソフトウェアであるphpunitに偶然出会いました。これを効果的に使用すると、ユニットテストの効率を大幅に向上させることができます。インターフェーストラバーサル。あまりナンセンスではありませんが、早速本題に入りましょう。

1. php ディレクトリ

にインストールします。2.

を設定します。まず、lib フォルダーに保存されている設定ファイル を作成します。

pear channel-discover pear; 
pear install phpunit/PHPUnit
最後に新しいbasetest.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;
}
これでインターフェイスのテスト環境がセットアップされました。

3. テストケースを書く

<?php 
require_once("transfer.php"); 
define("PREFIX", "http://xxx"); 
define("HTTPSPREFIX", "https://xxx"); 
 
function build_get_param($param) { 
    return http_build_query($param); 
}
投稿リクエストの場合は
<?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);
  }
この記事のケースを読んだ後は、その方法を習得したと思います。関連記事はPHP中国語サイトにあります!

推奨読書:

ThinkPHPによるWeChat支払い(jsapi支払い)プロセスの実装チュートリアルの詳細な説明_phpの例

PHPのWeChat返金申請

以上がphpunitインターフェースの自動テスト機能の実装の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。