LoginSignup
1
0

More than 3 years have passed since last update.

CodeceptionのQuickstartをやってみた

Posted at

EC CUBEのE2Eテストを書きたかったのでデファクトスタンダードを求めてコアを見たらCodeception - PHP Testing framework - PHP unit testing, PHP e2e testing, database testingっていうテスティングフレームワークを使っていたので試してみる。

https://codeception.com/quickstart に記載されている以上の情報は何もない。

新規プロジェクトを作成する

cd /path/to/your/directory
mkdir codeception
cd codeception
composer init -n

codeceptionをインストール

composer require "codeception/codeception" --dev

初期化

root@d2f73c532bee:/var/www/playground/codeception# php vendor/bin/codecept bootstrap
 Bootstrapping Codeception 

File codeception.yml created       <- global configuration
 Adding codeception/module-phpbrowser for PhpBrowser to composer.json
 Adding codeception/module-asserts for Asserts to composer.json
2 new packages added to require-dev
? composer.json updated. Do you want to run "composer update"? (y/n) y
 Running composer update


  [Composer\Json\JsonValidationException]                     
  "./composer.json" does not match the expected JSON schema:  
   - require : Array value found, but an object is required   


 Composer installation failed. Please check composer.json and try to run "composer update" manually
 Unit helper has been created in tests/_support/Helper

In ModuleContainer.php line 115:

  Module Asserts is not installed.                   
  Use Composer to install corresponding package:     

  composer require codeception/module-asserts --dev  


bootstrap [-s|--namespace [NAMESPACE]] [-a|--actor [ACTOR]] [-e|--empty] [--] [<path>]

「acceptance test」を作成する。
「acceptance test」は日本語で言うところの受入テストと言うことらしい。

root@d2f73c532bee:/var/www/playground/codeception# php vendor/bin/codecept generate:cest acceptance First

In Configuration.php line 327:

  Suite acceptance was not loaded  


generate:cest <suite> <class>

acceptanceって言うSuiteがないらしい。

Suiteってどこで定義されてるんだ

よく見たらcomposerのupdateでエラー出てるな

requireが配列になってたのでオブジェクトに直して再度bootstrap

bootstrapはforceオプションがない見たいなので設定ファイルを一旦全部消す

rm codeception.yml
rm -rf tests

retry

root@d2f73c532bee:/var/www/playground/codeception# php vendor/bin/codecept bootstrap
 Bootstrapping Codeception 

File codeception.yml created       <- global configuration
 Unit helper has been created in tests/_support/Helper

In ModuleContainer.php line 115:

  Module Asserts is not installed.                   
  Use Composer to install corresponding package:     

  composer require codeception/module-asserts --dev  


bootstrap [-s|--namespace [NAMESPACE]] [-a|--actor [ACTOR]] [-e|--empty] [--] [<path>]

update忘れてた

root@d2f73c532bee:/var/www/playground/codeception# composer update


  [Composer\Json\JsonValidationException]                     
  "./composer.json" does not match the expected JSON schema:  
   - require : Array value found, but an object is required   


またか、、、再度requireを修正してcomposer update

→うまくいった

retry2

root@d2f73c532bee:/var/www/playground/codeception# php vendor/bin/codecept bootstrap

In InitTemplate.php line 221:

  Codeception is already installed in this directory  


bootstrap [-s|--namespace [NAMESPACE]] [-a|--actor [ACTOR]] [-e|--empty] [--] [<path>]

もう一度削除して実行。。。

root@d2f73c532bee:/var/www/playground/codeception# rm codeception.yml
root@d2f73c532bee:/var/www/playground/codeception# rm -rf tests
root@d2f73c532bee:/var/www/playground/codeception# php vendor/bin/codecept bootstrap
 Bootstrapping Codeception 

File codeception.yml created       <- global configuration
 Unit helper has been created in tests/_support/Helper
 UnitTester actor has been created in tests/_support
 Actions have been loaded
tests/unit created                 <- unit tests
tests/unit.suite.yml written       <- unit tests suite configuration
 Functional helper has been created in tests/_support/Helper
 FunctionalTester actor has been created in tests/_support
 Actions have been loaded
tests/functional created           <- functional tests
tests/functional.suite.yml written <- functional tests suite configuration
 Acceptance helper has been created in tests/_support/Helper
 AcceptanceTester actor has been created in tests/_support
 Actions have been loaded
tests/acceptance created           <- acceptance tests
tests/acceptance.suite.yml written <- acceptance tests suite configuration
 --- 

 Codeception is installed for acceptance, functional, and unit testing 

Next steps:
1. Edit tests/acceptance.suite.yml to set url of your application. Change PhpBrowser to WebDriver to enable browser testing
2. Edit tests/functional.suite.yml to enable a framework module. Remove this file if you don't use a framework
3. Create your first acceptance tests using codecept g:cest acceptance First
4. Write first test in tests/acceptance/FirstCest.php
5. Run tests using: codecept run

できたっぽい。

root@d2f73c532bee:/var/www/playground/codeception# php vendor/bin/codecept generate:cest acceptance First
Test was created in /var/www/playground/codeception/tests/acceptance/FirstCest.php

できた。

Please make sure your local development server is running. Put application URL into: tests/acceptance.suite.yml

とのことなので、ターミナルで別タブを開いて開発サーバを立ち上げる

root@d2f73c532bee:/var/www/playground/codeception# php -S 127.0.0.1:8080
PHP 7.3.26-1+ubuntu18.04.1+deb.sury.org+1 Development Server started at Tue Jan 19 13:53:59 2021
Listening on http://127.0.0.1:8080
Document root is /var/www/playground/codeception
Press Ctrl-C to quit.

tests/acceptance.suite.yml に設定ファイルが追加されているのでurlhttp://localhost:8080/にする

tests/acceptance/FirstCest.phpに適当なテストをかく

class FirstCest
{
    public function _before(AcceptanceTester $I)
    {
    }

    // tests
    public function tryToTest(AcceptanceTester $I)
    {
    }

    public function frontpageWorks(AcceptanceTester $I)
    {
        $I->amOnPage('/');
        $I->see('Home');
    }
}

テストを実行する

root@d2f73c532bee:/var/www/playground/codeception# php vendor/bin/codecept run --steps
Codeception PHP Testing Framework v4.1.15
Powered by PHPUnit 9.5.1 by Sebastian Bergmann and contributors.

Acceptance Tests (2) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FirstCest: Try to test
Signature: FirstCest:tryToTest
Test: tests/acceptance/FirstCest.php:tryToTest
Scenario --
 PASSED 

FirstCest: Frontpage works
Signature: FirstCest:frontpageWorks
Test: tests/acceptance/FirstCest.php:frontpageWorks
Scenario --
 I am on page "/"
 I see "Home"
 FAIL 

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

Functional Tests (0) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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


Time: 00:00.366, Memory: 12.00 MB

There was 1 failure:

---------
1) FirstCest: Frontpage works
 Test  tests/acceptance/FirstCest.php:frontpageWorks
 Step  See "Home"
 Fail  Failed asserting that  on page /
--> 404 Not Found body { background-color: #fcfcfc; color: #333333; margin: 0; padding:0; } h1 { font-size: 1.5em; font-weight: normal; background-color: #9999cc; min-height:2em; line-height:2em; border-bottom: 1px inset black; margin: 0; } h1, p { padding-left: 10px; } code.url { background-color: #eee
[Content too long to display. See complete response in '/var/www/playground/codeception/tests/_output/' directory]
--> contains "Home".

Scenario Steps:

 2. $I->see("Home") at tests/acceptance/FirstCest.php:17
 1. $I->amOnPage("/") at tests/acceptance/FirstCest.php:16

Artifacts:

Html: /var/www/playground/codeception/tests/_output/FirstCest.frontpageWorks.fail.html
Response: /var/www/playground/codeception/tests/_output/FirstCest.frontpageWorks.fail.html

FAILURES!
Tests: 2, Assertions: 1, Failures: 1.

実行できた。

/にアクセスしたら404になったからエラーらしい。そりゃ何もないからね。

index.htmlを作成して再度実行

root@d2f73c532bee:/var/www/playground/codeception# echo 'hello' > index.html
root@d2f73c532bee:/var/www/playground/codeception# php vendor/bin/codecept run --steps
Codeception PHP Testing Framework v4.1.15
Powered by PHPUnit 9.5.1 by Sebastian Bergmann and contributors.

Acceptance Tests (2) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FirstCest: Try to test
Signature: FirstCest:tryToTest
Test: tests/acceptance/FirstCest.php:tryToTest
Scenario --
 PASSED 

FirstCest: Frontpage works
Signature: FirstCest:frontpageWorks
Test: tests/acceptance/FirstCest.php:frontpageWorks
Scenario --
 I am on page "/"
 I see "Home"
 FAIL 

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

Functional Tests (0) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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


Time: 00:00.314, Memory: 10.00 MB

There was 1 failure:

---------
1) FirstCest: Frontpage works
 Test  tests/acceptance/FirstCest.php:frontpageWorks
 Step  See "Home"
 Fail  Failed asserting that  on page /
--> hello 
--> contains "Home".

Scenario Steps:

 2. $I->see("Home") at tests/acceptance/FirstCest.php:17
 1. $I->amOnPage("/") at tests/acceptance/FirstCest.php:16

Artifacts:

Html: /var/www/playground/codeception/tests/_output/FirstCest.frontpageWorks.fail.html
Response: /var/www/playground/codeception/tests/_output/FirstCest.frontpageWorks.fail.html

FAILURES!
Tests: 2, Assertions: 1, Failures: 1.

エラーが変わった。Homeって言う文字列が含まれてないよって言う。

root@d2f73c532bee:/var/www/playground/codeception# echo 'Home' >> index.html 
root@d2f73c532bee:/var/www/playground/codeception# php vendor/bin/codecept run --steps
Codeception PHP Testing Framework v4.1.15
Powered by PHPUnit 9.5.1 by Sebastian Bergmann and contributors.

Acceptance Tests (2) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FirstCest: Try to test
Signature: FirstCest:tryToTest
Test: tests/acceptance/FirstCest.php:tryToTest
Scenario --
 PASSED 

FirstCest: Frontpage works
Signature: FirstCest:frontpageWorks
Test: tests/acceptance/FirstCest.php:frontpageWorks
Scenario --
 I am on page "/"
 I see "Home"
 PASSED 

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

Functional Tests (0) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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


Time: 00:00.228, Memory: 10.00 MB

OK (2 tests, 1 assertion)

Homeを追加して再度実行したらうまくいった。

超簡単・素晴らしい。

1
0
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
1
0