LoginSignup
56
58

More than 5 years have passed since last update.

Codeceptionというテストフレームワークを使ってPHPのテストをする

Last updated at Posted at 2016-04-12

Introduction

Codeceptionとは、PHPのテストを簡単に行えるテストフレームワークです。

公式ページのイントロダクションに、下記のように書いてあります。

Introduction - 英語版

The Codeception testing framework distinguishes these levels of testing. 
Out of the box you have tools for writing unit, functional, and acceptance tests in a single manner.

イントロダクション - 日本語版

Codeceptionのテストフレームワークはこれらのレベルを区別します。
単体テスト、機能テスト、受け入れテストを1つの方法ですぐに書けるようになるためのツールなのです。

便利そうなので使ってみます。

Version

  • Codeception 2.1.7
  • PhantomJS 2.1.1

Requirements

  • PHP >= 5.5.0
  • CURL enabled

composer本体のインストール

[参考]http://kore1server.com/175

$ curl -sS https://getcomposer.org/installer | php

# install先を指定する場合
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=bin

# パスを通す
$ sudo mv composer.phar /usr/local/bin/composer

composerでcodeceptionとPhantomJSをインストール

javascriptのテストをするためには、PhantomJSも必要。

  • composer.jsonに下記を記述
composer.json
{
    "require": {
        "codeception/codeception": "2.*",
        "jonnyw/php-phantomjs" : "4.*",
        "jakoch/phantomjs-installer": "2.1.1"
    },
    "scripts": {
        "post-install-cmd": [
            "PhantomInstaller\\Installer::installPhantomJS"
        ],
        "post-update-cmd": [
            "PhantomInstaller\\Installer::installPhantomJS"
        ]
    }
}
  • インストール
$ composer install

Codeception関連のファイル、ディレクトリを生成

$ php vendor/bin/codecept bootstrap

下記のようなファイルやディレクトリが生成されます。

codeception.yml
tests/

acceptance.suite.yml の設定

tests/acceptance.suite.ymlを下記のように記述

tests/acceptance.suite.yml
class_name: AcceptanceTester
modules:
    enabled: [WebDriver]
    config:
        WebDriver:
            url: 'http://サイトURL'
            browser: phantomjs
            capabilities:
                javascriptEnabled: true

テストコードを書いてみる (Acceptance Test - 受け入れテスト)

PhantomJSの動作検証も兼ねて、javascriptが絡むページのテストコードを書く

[参考] https://samsonasik.wordpress.com/2014/12/18/using-php-phantomjs-with-codeception/

下記のindex.htmlを作成し、webサーバーに設置

index.html
<html>
<body>

  <h1>Hello World!</h1>

  <button name="test-btn" id="test-btn">test-btn</button>

  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
       $("#test-btn").click(function() {
            $("h1").html("abcdef");
       });
    });
  </script> 

</body>
</html>

下記コマンドで、Sampleという名前の、Acceptance Testの雛形を生成する

$ php vendor/bin/codecept generate:cept acceptance Sample

生成されたファイルに、テストコードを書いていく。
h1が、#test-btnをクリックすることによって、Hello World!からabcdefに書き換わるかどうかのテスト。

tests/acceptance/SampleCept.php
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('see home page');
$I->amOnPage('/index.html');
$I->see('Hello World!');
$I->dontSee("abcdef", '//body/h1');
$I->click('#test-btn');
// $I->wait(1); // you can set delay
$I->See('abcdef','//body/h1');

Acceptance Test - 受け入れテスト

  • phantomjs 起動
$ vendor/bin/phantomjs --webdriver=4444
  • テスト実行
$ php vendor/bin/codecept run acceptance --steps --verbose

Codeception PHP Testing Framework v2.1.7
Powered by PHPUnit 5.3.1 by Sebastian Bergmann and contributors.

Acceptance Tests (1) -----------------------------------------------------------------------------
Modules: WebDriver
--------------------------------------------------------------------------------------------------
See home page (SampleCept)
Scenario:
* I am on page "/index.html"
* I see "Hello World!"
* I don't see "abcdef","//body/h1"
* I click "#test-btn"
* I see "abcdef","//body/h1"
 PASSED

--------------------------------------------------------------------------------------------------


Time: 1.53 seconds, Memory: 9.25Mb

OK (1 test, 3 assertions)

acceptance test(受け入れテスト)成功。

  • ログインのテストをする場合

SSLの証明書が正規のものでない場合、SslHandshakeFailedErrorというエラーが起きて処理が途中で止まってしまうので、下記のオプションを付けてPhanotmJSを起動する。

$ vendor/bin/phantomjs --webdriver=4444 --debug=true --web-security=no --ignore-ssl-errors=yes

tests/acceptance.suite.ymlで下記のように書いてもダメだったので、
PhantomJSの起動オプションで指定する。

tests/acceptance.suite.yml
# なぜかコレだと適用されない
phantomjs.cli.args: ['--web-security=no', '--ignore-ssl-errors=yes']

追記

もしPhantomJS起動時に、下記のエラーが出る場合は、fontconfig-develをインストール。
vendor/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory

$ sudo yum -y install fontconfig-devel



unit test(単体テスト)、functional test(機能テスト)はまた別途。
以上

[codeception マニュアル]
トップページ:http://codeception.com/
日本語:http://piccagliani.github.io/Codeception.docs.ja_JP/index.html

[参考]
Using PHP Phantomjs with Codeception
Modern Testing in PHP な Codeception を触ってみた1
Modern Testing in PHP な Codeception を触ってみた2

[関連記事]
CentOS6にseleniumとfirefoxのインストール

56
58
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
56
58