Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

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

Ippei Sumida

July 15, 2017
Tweet

More Decks by Ippei Sumida

Other Decks in Programming

Transcript

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

    $I->fillField('#inquiry_email', '[email protected]'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title'); $I->canSeeInDatabase('inquiry', [ 'name' => '͢Έͩɹ͍ͬ΃͍', 'email' => '[email protected]', 'question' => '͓໰͍߹Θͤ಺༰', ]); } 1.Codeceptionͱ͸
  2. 1.Codeceptionͱ͸ 6OJU w ֤Ϋϥεɺϝιου w 1)16OJUΠϯετʔϧ
 ෆཁ 1)16OJUΛར༻ͨ͠
 ୯ମςετ 'VODUJPOBM

    w ػೳ୯Ґ w ϦΫΤετʹର͢Δ
 ݁ՌΛ֬ೝ w ֤'8ରԠ 8FCαʔόΛհ͞ͳ ͍'8ςετ "DDFQUBODF w ػೳ୯Ґ w ը໘্ͷಈ࡞֬ೝ w ϒϥ΢βґଘ ϒϥ΢βΛར༻ͨ͠ डೖςετ
  3. 2.CodeceptionͰͷςετ࣮૷ 6OJU w ֤Ϋϥεɺϝιου w 1)16OJUΠϯετʔϧ
 ෆཁ 1)16OJUΛར༻ͨ͠
 ୯ମςετ 'VODUJPOBM

    w ػೳ୯Ґ w ϦΫΤετʹର͢Δ
 ݁ՌΛ֬ೝ w ֤'8ରԠ 8FCαʔόΛհ͞ͳ ͍'8ςετ "DDFQUBODF w ػೳ୯Ґ w ը໘্ͷಈ࡞֬ೝ w ϒϥ΢βґଘ ϒϥ΢βΛར༻ͨ͠ डೖςετ
  4. 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ʣ
  5. 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ʣ
  6. 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ʣ ͲͷςετͰΤϥʔʹͳ͔ͬͨग़ྗ
  7. 2.CodeceptionͰͷςετ࣮૷ unit.suite.yml # Codeception Test Suite Configuration # # Suite

    for unit or integration tests. actor: UnitTester modules: enabled: - Asserts - \Helper\Unit
  8. 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'
  9. 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
  10. 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
  11. 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
  12. 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 ϒϥ΢βͷઃఆ
  13. ϒϥ΢βࢦఆ 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
  14. 2.CodeceptionͰͷςετ࣮૷ $FQU w ্͔Βॱʹ࣮ߦ w ϑΝΠϧγφϦΦ w ؆୯ͳγφϦΦ޲͖ γφϦΦϑΝΠϧ $FTU

    w 1)16OJUͷ༷ʹهड़ w ϝιουγφϦΦ w ෳࡶͳγφϦΦ0, γφϦΦΫϥε
  15. 2.CodeceptionͰͷςετ࣮૷ Cept <?php $I = new FunctionalTester($scenario); $I->wantTo('͓໰͍߹Θͤొ࿥ςετ'); $I->amOnPage('/inquiry'); $I->canSee('͓໰͍߹Θͤ',

    '#title'); $I->fillField('#inquiry_name', '͢Έͩɹ͍ͬ΃͍'); $I->fillField('#inquiry_email', '[email protected]'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title');
  16. 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', '[email protected]'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͓໰͍߹Θͤ׬ྃ͠·ͨ͠ɻ', '#title'); } }
  17. <?php class InquiryFunctionalCest { public function _before(FunctionalTester $I) { }

    public function _after(FunctionalTester $I) { } // tests } 2.CodeceptionͰͷςετ࣮૷ Cest લॲཧʢ@CFGPSF ޙॲཧʢ@BGUFS
  18. 2.CodeceptionͰͷςετ࣮૷ $FQU w ্͔Βॱʹ࣮ߦ w ϑΝΠϧγφϦΦ w ؆୯ͳγφϦΦ޲͖ γφϦΦϑΝΠϧ $FTU

    w 1)16OJUͷ༷ʹهड़ w ϝιουγφϦΦ w ෳࡶͳγφϦΦ0, γφϦΦΫϥε ɹ Φ ε ε ϝ ʂ ɹ
  19. ϝιου උߟ ࢦఆͷϖʔδʹҠಈ *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Ͱࢦఆ Α͘࢖͏ϝιου
  20. ϝιου උߟ ςΩετϘοΫεʹॻ͘ *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ʣ
  21. ϝιου උߟ %#ʹࢦఆͷσʔλ͕͋ Δ͔֬ೝ *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ʣ
  22. 2.CodeceptionͰͷςετ࣮૷ /** * ొ࿥ςετ * * @param AcceptanceTester $I */

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

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

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

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

    chrome */ public function tryToTest(AcceptanceTester $I) { // env=firefox or chrome͚ͩςετ } ΞϊςʔγϣϯͰ؀ڥΛࢦఆ 3.Codeception Tips
  28. 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 ΢Οϯυ΢αΠζมߋ
  29. 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
  30. 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
  31. 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
  32. /** * ॏෳొ࿥ςετ * @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', '[email protected]'); $I->fillField('#inquiry_question', '͓໰͍߹Θͤ಺༰'); $I->click('ૹ৴'); $I->canSee('͜ͷϝʔϧΞυϨε͸͢Ͱʹ࢖༻͞Ε͍ͯ·͢'); } 3.Codeception Tips
  33. /** * ॏෳςετ༻ͷσʔλΛ࡞੒͢Δ * * @param AcceptanceTester $I */ protected

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