CodeceptionでREST APIの受け入れテストを作成
CodeceptionではREST APIの受け入れテストも可能ですので紹介します。
目次
REST APIの受け入れテスト手順
プロジェクトの作成
実行ファイルがある場所へ移動して雛型を作成します。そうするとtests配下にapi(任意)のプロジェクトが作成されます。
$ cd Codeception $ php codecept generate:suite api Helper \Helper\Api was created in /home/hoge-user/Codeception/tests/support/Helper/Api.php Actor ApiTester was created in /home/hoge-user/Codeception/tests/support/ApiTester.php Suite config api.suite.yml was created. Next steps: 1. Edit api.suite.yml to enable modules for this suite 2. Create first test with generate:cest testName ( or test|cept) command 3. Run tests of this suite with codecept run api command Suite api generated
試しにプロジェクトの実行
runすればすべてのプロジェクトのテストを実行できます。今回はapiのみを実行してみます。
まだ何もシナリオを記述してないのでテスト実施結果は空です。
$ php codecept run api Codeception PHP Testing Framework v2.3.8 Powered by PHPUnit 6.5.6 by Sebastian Bergmann and contributors. Api Tests (0) ------------- -------------------------- Time: 59 ms, Memory: 8.00MB
アクターを記述
プロジェクト作成時に以下のようにアクターが自動生成されます。
actor: ApiTester modules: enabled: - \Helper\Api
これを以下のように修正します。
url:接続先
depends: 接続時にPhpBrowserを利用
part: デフォルトはXMLとJsonを受け取る。どちらか片方のみしか受け取らない場合、明示が可能。
actor: ApiTester modules: enabled: - REST: url: http://localhost/ depends: PhpBrowser part: Json
受け入れテスト作成
以下コマンドを実行してapiにCept形式の受け入れテストを生成します。
$ php codecept generate:cept api SampleTest Test was created in /home/hoge-user/Codeception/tests/api/SampleTestCept.php $ vim tests/api/SampleTestCept.php
今回はGETでアクセスして、200応答が返ってくることを確認してみます。
<?php $I = new ApiTester($scenario); $I->wantTo('perform actions and see result'); $I->sendGET('/'); $I->seeResponseCodeIs(200);
事前にnginxを立てておきます。
$ sudo yum install -y nginx $ sudo service nginx restart
受け入れテスト実行
ではapiプロジェクトを実行してみましょう。
$ php codecept run api Codeception PHP Testing Framework v2.3.8 Powered by PHPUnit 6.5.6 by Sebastian Bergmann and contributors. Api Tests (1) ---------------------------------------------------- ✔ SampleTestCept: Perform actions and see result (0.01s) ------------------------------------------------------------------ Time: 80 ms, Memory: 10.00MB OK (1 test, 0 assertions)
wantToで記述されていた内容が表示されているのが分かるかと思います。
また以下のように「--steps」を付与することで詳細結果を表示することも可能です。
$ php codecept run api --steps Api Tests (1) --------------------------------------- SampleTestCept: Perform actions and see result Signature: SampleTestCept Test: tests/api/SampleTestCept.php Scenario -- I send get "/" I see response code is 200 PASSED ----------------------------------------------------- Time: 76 ms, Memory: 10.00MB
debugモードでより詳細に。
$ php codecept run api --debug Codeception PHP Testing Framework v2.3.8 Powered by PHPUnit 6.5.6 by Sebastian Bergmann and contributors. Api Tests (1) ---------------------------------------- Modules: REST, PhpBrowser ------------------------------------------------------ SampleTestCept: Perform actions and see result Signature: SampleTestCept Test: tests/api/SampleTestCept.php Scenario -- I send get "/" [Request] GET http://localhost// [Request Headers] [] [Page] http://localhost// [Response] 200 [Request Cookies] [] [Response Headers] {"Server":["nginx/1.12.1"],"Date":["Wed, 14 Feb 2018 15:50:51 GMT"],"Content-Type":["text/html;charset=UTF-8"],"Content-Length":["3770"],"Last-Modified":["Mon, 11 Sep 2017 19:56:09 GMT"],"Connection":["keep-alive"],"ETag":[""59b6ea59-eba""],"Accept-Ranges":["bytes"]} [Response] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> ・・・省略・・・