テストと仲良くするためのCodeceptionのはじめかた #phpkansai

テストと仲良くするためのCodeceptionのはじめかた #phpkansai

PHPカンファレンス関西 2017の発表資料です。

A73ed5cefbbd84abd2c5e14a5eb4a339?s=128

Ippei Sumida

July 15, 2017
Tweet

Transcript

  1. 7.
  2. 8.
  3. 9.
  4. 17.

    public function registerTest(AcceptanceTester $I) { $I->wantToTest('ొ࿥ςετ'); $I->canSee('͓໰͍߹Θͤ', '#title'); $I->fillField('#inquiry_name', '͢Έͩɹ͍ͬ΃͍');

    $I->fillField('#inquiry_email', 'ippey.s@gmail.com'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title'); $I->canSeeInDatabase('inquiry', [ 'name' => '͢Έͩɹ͍ͬ΃͍', 'email' => 'ippey.s@gmail.com', 'question' => '͓໰͍߹Θͤ಺༰', ]); } 1.Codeceptionͱ͸
  5. 19.

    1.Codeceptionͱ͸ 6OJU w ֤Ϋϥεɺϝιου w 1)16OJUΠϯετʔϧ
 ෆཁ 1)16OJUΛར༻ͨ͠
 ୯ମςετ 'VODUJPOBM

    w ػೳ୯Ґ w ϦΫΤετʹର͢Δ
 ݁ՌΛ֬ೝ w ֤'8ରԠ 8FCαʔόΛհ͞ͳ ͍'8ςετ "DDFQUBODF w ػೳ୯Ґ w ը໘্ͷಈ࡞֬ೝ w ϒϥ΢βґଘ ϒϥ΢βΛར༻ͨ͠ डೖςετ
  6. 27.

    2.CodeceptionͰͷςετ࣮૷ 6OJU w ֤Ϋϥεɺϝιου w 1)16OJUΠϯετʔϧ
 ෆཁ 1)16OJUΛར༻ͨ͠
 ୯ମςετ 'VODUJPOBM

    w ػೳ୯Ґ w ϦΫΤετʹର͢Δ
 ݁ՌΛ֬ೝ w ֤'8ରԠ 8FCαʔόΛհ͞ͳ ͍'8ςετ "DDFQUBODF w ػೳ୯Ґ w ը໘্ͷಈ࡞֬ೝ w ϒϥ΢βґଘ ϒϥ΢βΛར༻ͨ͠ डೖςετ
  7. 39.

    2.CodeceptionͰͷςετ࣮૷ Acceptance (phpbrowser) Tests (2) --------------------------------------------------------------------- --------------------------------------------------------------------- --------------------- Testing acceptance

    (phpbrowser) ✔ InquiryCest: Test ొ࿥ςετ (1.30s) ✔ InquiryCest: Test ॏෳొ࿥ςετ (1.04s) --------------------------------------------------------------------- --------------------------------------------------------------------- ------------------------------------------------------- Time: 2.59 seconds, Memory: 17.00MB OK (2 tests, 5 assertions) ࣮ߦ݁ՌʢOKʣ
  8. 40.

    2.CodeceptionͰͷςετ࣮૷ Acceptance (phpbrowser) Tests (2) --------------------------------------------------------------------- --------------------------------------------------------------------- --------------------- Testing acceptance

    (phpbrowser) ✖ InquiryCest: Test ొ࿥ςετ ✔ InquiryCest: Test ొ࿥ςετ[F] (2.10s) ✔ InquiryCest: Test ॏෳొ࿥ςετ (1.30s) --------------------------------------------------------------------- --------------------------------------------------------------------- ------------------------------------------------------- Time: 3.7 seconds, Memory: 17.25MB There was 1 failure: ࣮ߦ݁ՌʢNGʣ
  9. 41.

    2.CodeceptionͰͷςετ࣮૷ --------- 1) InquiryCest: Test ొ࿥ςετ Test tests/acceptance/InquiryCest.php:registerTest Failed asserting

    that any element by '#title' on page /inquiry/ + <h1 id="title">͓໰͍߹Θͤ</h1> contains text 'a͓໰͍߹Θͤ' ࣮ߦ݁ՌʢNGʣ ͲͷςετͰΤϥʔʹͳ͔ͬͨग़ྗ
  10. 45.

    2.CodeceptionͰͷςετ࣮૷ unit.suite.yml # Codeception Test Suite Configuration # # Suite

    for unit or integration tests. actor: UnitTester modules: enabled: - Asserts - \Helper\Unit
  11. 48.

    2.CodeceptionͰͷςετ࣮૷ functional.suite.yml actor: FunctionalTester modules: enabled: # add a framework

    module here - \Helper\Functional - Symfony: app_path: 'app' var_path: 'var' - Db: dsn: 'sqlite:var/data.sqlite' user: '' password: '' populate: true dump: 'tests/_data/dump.sql'
  12. 49.

    actor: FunctionalTester modules: enabled: # add a framework module here

    - \Helper\Functional - Symfony: app_path: 'app' var_path: 'var' - Db: dsn: 'sqlite:var/data.sqlite' user: '' password: '' populate: true dump: 'tests/_data/dump.sql' 2.CodeceptionͰͷςετ࣮૷ ϑϨʔϜϫʔΫͷઃఆ functional.suite.yml
  13. 50.

    actor: FunctionalTester modules: enabled: # add a framework module here

    - \Helper\Functional - Symfony: app_path: 'app' var_path: 'var' - Db: dsn: 'sqlite:var/data.sqlite' user: '' password: '' populate: true dump: 'tests/_data/dump.sql' 2.CodeceptionͰͷςετ࣮૷ %#ͷઃఆ functional.suite.yml
  14. 53.

    2.CodeceptionͰͷςετ࣮૷ acceptance.suite.yml actor: AcceptanceTester modules: enabled: - WebDriver: url: http://localhost:8000

    browser: phantomjs - \Helper\Acceptance - Db: dsn: 'sqlite:var/data.sqlite' user: '' password: '' populate: true dump: 'tests/_data/dump.sql' env: firefox: modules: config: WebDriver: browser: firefox
  15. 54.

    actor: AcceptanceTester modules: enabled: - WebDriver: url: http://localhost:8000 browser: phantomjs

    - \Helper\Acceptance - Db: dsn: 'sqlite:var/data.sqlite' user: '' password: '' populate: true dump: 'tests/_data/dump.sql' env: firefox: modules: config: WebDriver: browser: firefox 2.CodeceptionͰͷςετ࣮૷ acceptance.suite.yml ϒϥ΢βͷઃఆ
  16. 57.

    ϒϥ΢βࢦఆ PHPBrowser PhantomJS firefox/chrome 2.CodeceptionͰͷςετ࣮૷ modules: enabled: - PhpBrowser: url:

    http://localhost:8000 modules: enabled: - WebDriver: url: http://localhost:8000 browser: phantomjs modules: enabled: - WebDriver: url: http://localhost:8000 browser: firefox
  17. 61.

    2.CodeceptionͰͷςετ࣮૷ $FQU w ্͔Βॱʹ࣮ߦ w ϑΝΠϧγφϦΦ w ؆୯ͳγφϦΦ޲͖ γφϦΦϑΝΠϧ $FTU

    w 1)16OJUͷ༷ʹهड़ w ϝιουγφϦΦ w ෳࡶͳγφϦΦ0, γφϦΦΫϥε
  18. 62.

    2.CodeceptionͰͷςετ࣮૷ Cept <?php $I = new FunctionalTester($scenario); $I->wantTo('͓໰͍߹Θͤొ࿥ςετ'); $I->amOnPage('/inquiry'); $I->canSee('͓໰͍߹Θͤ',

    '#title'); $I->fillField('#inquiry_name', '͢Έͩɹ͍ͬ΃͍'); $I->fillField('#inquiry_email', 'hogehoge@gmail.com'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title');
  19. 63.

    2.CodeceptionͰͷςετ࣮૷ Cest <?php class InquiryFunctionalCest { // tests public function

    registrationTest(FunctionalTester $I) { $I->amOnPage('/inquiry'); $I->canSee('͓໰͍߹Θͤ', '#title'); $I->fillField('#inquiry_name', '͢Έͩɹ͍ͬ΃͍'); $I->fillField('#inquiry_email', 'ippey.s@gmail.com'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title'); } }
  20. 64.

    <?php class InquiryFunctionalCest { public function _before(FunctionalTester $I) { }

    public function _after(FunctionalTester $I) { } // tests } 2.CodeceptionͰͷςετ࣮૷ Cest લॲཧʢ@CFGPSF ޙॲཧʢ@BGUFS
  21. 65.

    2.CodeceptionͰͷςετ࣮૷ $FQU w ্͔Βॱʹ࣮ߦ w ϑΝΠϧγφϦΦ w ؆୯ͳγφϦΦ޲͖ γφϦΦϑΝΠϧ $FTU

    w 1)16OJUͷ༷ʹهड़ w ϝιουγφϦΦ w ෳࡶͳγφϦΦ0, γφϦΦΫϥε ɹ Φ ε ε ϝ ʂ ɹ
  22. 66.

    ϝιου උߟ ࢦఆͷϖʔδʹҠಈ *BN0O1BHF l63-z  BDDFQUBODFTVJUFZNM ʹࢦఆͨ͠63-ʹ௥Ճ͞ ΕͯΞΫηεɻ ϖʔδʹࢦఆͷจࣈ͕͋

    Δ͔ *TFF lจࣈྻz  *TFF lจࣈྻz lཁ ૉz  *DBO4FF lจࣈྻz  DBOΛ͚ͭΔͱݟ͔ͭΒ ͳͯ͘΋ςετ͕ࢭ·Β ͳ͍ ϖʔδʹࢦఆͷจࣈ͕ͳ ͍͔ *EPOU4FF lจࣈྻz  *EPOU4FF lจࣈྻz  lཁૉz  *DBOU4FF lจࣈྻz  DBOΛ͚ͭΔͱݟ͔ͭΒ ͳͯ͘΋ςετ͕ࢭ·Β ͳ͍ ΫϦοΫ *DMJDL lϩάΠϯz  *DMJDL lMPHJOz  จࣈྻ 9QBUI $44 4FMFDUPSͰࢦఆ Α͘࢖͏ϝιου
  23. 67.

    ϝιου උߟ ςΩετϘοΫεʹॻ͘ *pMM'JFME lOBNFz  lWBMVFz  *pMM'JFME lOBNFz

     lWBMVFz  ϑΟʔϧυ ໊ 9QBUI $444FMFDUPS Ͱࢦఆ ϥδΦϘλϯબ୒ * DIFDL0QUJPO lOBNFz  lWBMVFz  * ϑΟʔϧυ ໊ 9QBUI $444FMFDUPS Ͱࢦఆ ηϨΫτϘοΫεɺ νΣοΫϘοΫεબ୒ * TFMFDU0QUJPO lOBNFz  lWBMVFz  * ϑΟʔϧυ ໊ 9QBUI $444FMFDUPS Ͱࢦఆ ·ͱΊͯϑΥʔϜૹ৴ * TVCNJU'PSN bGPSN`  <bOBNF`bWBMVF`>  ϑΟʔϧυ ໊ 9QBUI $444FMFDUPS Ͱࢦఆ Α͘࢖͏ϝιουʢFormʣ
  24. 68.

    ϝιου උߟ %#ʹࢦఆͷσʔλ͕͋ Δ͔֬ೝ *TFF*O%BUBCBTF bςʔϒϧ ໊` <bϑΟʔϧυ໊`b஋`>  DBOΛ͚ͭΔͱݟ͔ͭΒ

    ͳͯ͘΋ςετ͕ࢭ·Β ͳ͍ %#ʹࢦఆͷσʔλ͕ͳ ͍͔֬ೝ *EPOU4FF*O%BUBCBTF bςʔ ϒϧ໊` <bϑΟʔϧυ໊`b஋`>  DBOΛ͚ͭΔͱݟ͔ͭͬ ͯ΋ςετ͕ࢭ·Βͳ͍ σʔλΛొ࿥͢Δ *IBWF*O%BUBCBTF bςʔϒϧ ໊` <bϑΟʔϧυ໊`b஋`>  ςετσʔλ࡞੒࣌ʹ σʔλΛऔಘ͢Δ *HSBC'SPN%BUBCBTF  bςʔϒϧ໊` bऔಘϑΟʔϧυ໊`  <bϑΟʔϧυ໊`b஋`>  Α͘࢖͏ϝιουʢDBʣ
  25. 69.

    2.CodeceptionͰͷςετ࣮૷ /** * ొ࿥ςετ * * @param AcceptanceTester $I */

    public function registerTest(AcceptanceTester $I) { $I->wantToTest('ొ࿥ςετ'); $I->canSee('͓໰͍߹Θͤ', '#title'); $I->fillField('#inquiry_name', '͢Έͩɹ͍ͬ΃͍'); $I->fillField('#inquiry_email', 'ippey.s@gmail.com'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title'); $I->canSeeInDatabase('inquiry', [ 'name' => '͢Έͩɹ͍ͬ΃͍', 'email' => 'ippey.s@gmail.com', 'question' => '͓໰͍߹Θͤ಺༰', ]); }
  26. 70.

    /** * ొ࿥ςετ * * @param AcceptanceTester $I */ public

    function registerTest(AcceptanceTester $I) { $I->wantToTest('ొ࿥ςετ'); $I->canSee('͓໰͍߹Θͤ', '#title'); $I->fillField('#inquiry_name', '͢Έͩɹ͍ͬ΃͍'); $I->fillField('#inquiry_email', 'ippey.s@gmail.com'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title'); $I->canSeeInDatabase('inquiry', [ 'name' => '͢Έͩɹ͍ͬ΃͍', 'email' => 'ippey.s@gmail.com', 'question' => '͓໰͍߹Θͤ಺༰', ]); } 2.CodeceptionͰͷςετ࣮૷ JElUJUMFzʹʰ͓໰͍߹Θͤʱ ͱग़ྗ͞Ε͍ͯΔ͔ʁ
  27. 71.

    /** * ొ࿥ςετ * * @param AcceptanceTester $I */ public

    function registerTest(AcceptanceTester $I) { $I->wantToTest('ొ࿥ςετ'); $I->canSee('͓໰͍߹Θͤ', '#title'); $I->fillField('#inquiry_name', '͢Έͩɹ͍ͬ΃͍'); $I->fillField('#inquiry_email', 'ippey.s@gmail.com'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title'); $I->canSeeInDatabase('inquiry', [ 'name' => '͢Έͩɹ͍ͬ΃͍', 'email' => 'ippey.s@gmail.com', 'question' => '͓໰͍߹Θͤ಺༰', ]); } 2.CodeceptionͰͷςετ࣮૷ JElJORVJSZ@OBNFzʹʰ͢Έͩɹ͍ͬ΃͍ʱ Λೖྗ͢Δ
  28. 72.

    /** * ొ࿥ςετ * * @param AcceptanceTester $I */ public

    function registerTest(AcceptanceTester $I) { $I->wantToTest('ొ࿥ςετ'); $I->canSee('͓໰͍߹Θͤ', '#title'); $I->fillField('#inquiry_name', '͢Έͩɹ͍ͬ΃͍'); $I->fillField('#inquiry_email', 'ippey.s@gmail.com'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title'); $I->canSeeInDatabase('inquiry', [ 'name' => '͢Έͩɹ͍ͬ΃͍', 'email' => 'ippey.s@gmail.com', 'question' => '͓໰͍߹Θͤ಺༰', ]); } 2.CodeceptionͰͷςετ࣮૷ l*ORVJSZzςʔϒϧʹ ࢦఆͷ಺༰Ͱొ࿥͞Εͨ
 σʔλ͕͋Δ͔֬ೝ
  29. 78.

    actor: AcceptanceTester modules: enabled: - WebDriver: url: http://localhost:8000 browser: phantomjs

    - \Helper\Acceptance - Db: dsn: 'sqlite:var/data.sqlite' user: '' password: '' populate: true dump: 'tests/_data/dump.sql' env: firefox: modules: config: WebDriver: browser: firefox ࢦఆͨ͠಺༰Ͱ্ॻ͖ ͜͜Ͱ͸CSPXTFSΛpSFGPYʹ্ॻ͖ 3.Codeception Tips
  30. 80.

    /** * @param AcceptanceTester $I * @env firefox * @env

    chrome */ public function tryToTest(AcceptanceTester $I) { // env=firefox or chrome͚ͩςετ } ΞϊςʔγϣϯͰ؀ڥΛࢦఆ 3.Codeception Tips
  31. 81.

    actor: AcceptanceTester modules: enabled: - WebDriver: url: http://localhost:8000 browser: phantomjs

    - \Helper\Acceptance - Db: dsn: 'sqlite:var/data.sqlite' user: '' password: '' populate: true dump: 'tests/_data/dump.sql' env: firefox: modules: config: WebDriver: browser: firefox window_size: 1024x768 3.Codeception Tips ΢Οϯυ΢αΠζมߋ
  32. 83.

    actor: AcceptanceTester modules: enabled: - WebDriver: url: http://localhost:8000 browser: phantomjs

    - \Helper\Acceptance env: firefox: modules: config: WebDriver: browser: firefox phpbrowser: modules: enabled: - PhpBrowser: url: http://localhost:8000 ͜Εͩͱ--env=phpbrowserͰΤϥʔʂ 3.Codeception Tips
  33. 84.

    actor: AcceptanceTester modules: enabled: - WebDriver: url: http://localhost:8000 browser: phantomjs

    - \Helper\Acceptance env: firefox: modules: config: WebDriver: browser: firefox phpbrowser: modules: enabled: - PhpBrowser: url: http://localhost:8000 ͜Εͩͱ--env=phpbrowserͰΤϥʔʂ ͲͪΒͷϒϥ΢βͰςετ͢Ε͹
 Α͍͔Θ͔Βͳ͘ͳ͍ͬͯΔɻ 3.Codeception Tips
  34. 85.

    actor: AcceptanceTester modules: enabled: - WebDriver: url: http://localhost:8000 browser: phantomjs

    - \Helper\Acceptance env: firefox: modules: config: WebDriver: browser: firefox phpbrowser: modules: disabled: - WebDriver enabled: - PhpBrowser: url: http://localhost:8000 EJTBCMFEͰແޮԽ 3.Codeception Tips
  35. 92.

    /** * ॏෳొ࿥ςετ * @param AcceptanceTester $I * @before createData

    * @after afterMethod */ public function duplicateTest(AcceptanceTester $I) { $I->wantToTest('ॏෳొ࿥ςετ'); $I->canSee('͓໰͍߹Θͤ', '#title'); $I->fillField('#inquiry_name', '͢Έͩɹ͍ͬ΃͍'); $I->fillField('#inquiry_email', 'ippey.s@gmail.com'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͜ͷϝʔϧΞυϨε͸͢Ͱʹ࢖༻͞Ε͍ͯ·͢'); } 3.Codeception Tips
  36. 93.

    /** * ॏෳςετ༻ͷσʔλΛ࡞੒͢Δ * * @param AcceptanceTester $I */ protected

    function createData(AcceptanceTester $I) { $I->haveInDatabase('inquiry', [ 'name' => '͢Έͩɹ͍ͬ΃͍', 'email' => 'ippey.s@gmail.com', 'question' => '͓໰͍߹Θͤ಺༰', 'created_at' => \Carbon\Carbon::now() ]); } 3.Codeception Tips લɾޙॲཧ༻ϝιου͸”protected”