LoginSignup
7

More than 5 years have passed since last update.

CodeCeption + Laravel5 のメモ

Last updated at Posted at 2016-07-07

個人的なメモ書き。Larave5 のプロジェクトで CodeCeptionでテストしながら作っていく形

Version

  1. laravel/framework: 5.2.*
  2. codeception/codeception: 2.2

Install

よく使うものをインストール

  1. composer require laravelcollective/html
  2. composer require barryvdh/laravel-ide-helper
  3. composer require doctrine/dbal
  4. composer require barryvdh/laravel-debugbar

テスト環境のパッケージインストール

  1. composer require codeception/codeception --dev
  2. composer require site5/phantoman --dev
  3. composer require codeception/phpbuiltinserver --dev

テスト環境を作成

CodeCeptionに向いたテスト環境にはなってないので、CodeCeptionでテスト環境を作成

  1. ./vendor/bin/codecept bootstrap

Welcomeページのテスト

Functional テストで、Welcome が出るか確認する為のテストを作成。

  1. ./vendor/bin/codecept generate:cept functional WelcomePageCept

作成されたファイルの中身を少し変更。

<?php 
$I = new FunctionalTester($scenario);
$I->wantTo('perform actions and see result');
$I->amOnPage('/');
$I->see('laravel');

そしておもむろに実行する。

./vendor/bin/codecept run  
Codeception PHP Testing Framework v2.2.2
Powered by PHPUnit 4.8.26 by Sebastian Bergmann and contributors.

Acceptance Tests (0) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Functional Tests (1) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
E WelcomePageCept: Perform actions and see result (0.0s)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Unit Tests (0) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Time: 334 ms, Memory: 12.00MB

There was 1 error:

---------
1) WelcomePageCept: Perform actions and see result
 Test  tests/functional/WelcomePageCept.php

  [RuntimeException] Call to undefined method FunctionalTester::amOnPage  

#1  ./tests/functional/WelcomePageCept.php:4

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

おこなの。

CodeCeptionで、Laravel5 のモジュールを読んでいないので、読ませるようにします。

tests/functional.suite.yml
# Codeception Test Suite Configuration
#
# Suite for functional (integration) tests
# Emulate web requests and make application process them
# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it

class_name: FunctionalTester
modules:
    enabled:
        # add framework module here
        - \Helper\Functional
        - Laravel5 # この行を追加

そして実行。

./vendor/bin/codecept run    
Codeception PHP Testing Framework v2.2.2
Powered by PHPUnit 4.8.26 by Sebastian Bergmann and contributors.

Acceptance Tests (0) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Functional Tests (1) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- WelcomePageCept: Perform actions and see result

  [PDOException]                             
  SQLSTATE[HY000] [2002] Connection refused  


run [--report] [--html [HTML]] [--xml [XML]] [--tap [TAP]] [--json [JSON]] [--colors] [--no-colors] [--silent] [--steps] [-d|--debug] [--coverage [COVERAGE]] [--coverage-html [COVERAGE-HTML]] [--coverage-xml [COVERAGE-XML]] [--coverage-text [COVERAGE-TEXT]] [--no-exit] [-g|--group GROUP] [-s|--skip SKIP] [-x|--skip-group SKIP-GROUP] [--env ENV] [-f|--fail-fast] [--no-rebuild] [--] [<suite>] [<test>]


DB 周りで怒られてますので設定してやります。

テスト環境のデータベースまわり

テスト環境なので、.env.env.testing にコピーして、下記のように、DB_CONNECTIONだけを残して値を変更します。

env.testing
DB_CONNECTION=sqlite_testing

そして、sqlite をメモリー上で動くようにします。

config/database.php
<?php

return [
    'fetch' => PDO::FETCH_CLASS,
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        // テスト用の設定
        'sqlite_testing' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
    ],
    'migrations' => 'migrations',
    'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

];

環境設定を .env を読み込んでいるので、.env.testing を読ませるようにします。

tests/functional.suite.yml
# Codeception Test Suite Configuration
#
# Suite for functional (integration) tests
# Emulate web requests and make application process them
# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it

class_name: FunctionalTester
modules:
    enabled:
        # add framework module here
        - \Helper\Functional
        - Laravel5: # この行を追加
            environment_file: .env.testing

そして実行。

$ ./vendor/bin/codecept run                            
Codeception PHP Testing Framework v2.2.2
Powered by PHPUnit 4.8.26 by Sebastian Bergmann and contributors.

Acceptance Tests (0) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Functional Tests (1) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
✔ WelcomePageCept: Perform actions and see result (0.7s)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Unit Tests (0) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Time: 748 ms, Memory: 18.00MB

OK (1 test, 1 assertion)

一番初めのテストはOKになりました。

とりあえずここまで。

参考

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
7