ホームページ  >  記事  >  バックエンド開発  >  phpunitのインターフェース自動化手順の詳細な説明

phpunitのインターフェース自動化手順の詳細な説明

php中世界最好的语言
php中世界最好的语言オリジナル
2018-05-17 15:08:251544ブラウズ

今回は 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[&#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);
  }
この記事のケースを読んだ後は、その方法を習得したと思います。関連記事はPHP中国語サイトにあります!

推奨読書:

ThinkPHPフレームワークでのユーザー情報の更新と削除手順の詳細な説明

ThinkPHP接続データベース運用事例分析

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

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