Testing with Codeception

  • 10,812 views
Uploaded on

Talk on Testing with Codeception presented at PHPNW April 2013, DPC 2013 Uncon, PFCongress 2013, PHPBNL 2014, DPC 2014, PHPNW 2014

Talk on Testing with Codeception presented at PHPNW April 2013, DPC 2013 Uncon, PFCongress 2013, PHPBNL 2014, DPC 2014, PHPNW 2014

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • anybody using codeception for Magento?
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
10,812
On Slideshare
0
From Embeds
0
Number of Embeds
21

Actions

Shares
Downloads
71
Comments
1
Likes
16

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Testing with Codeception Jeremy Coates / @phpcodemonkey @magma_digital Green Refactor Red
  • 2. Who am I? ๏ Jeremy Coates, MD at Magma Digital Ltd! ๏ Founder of PHPNW! ๏ @phpcodemonkey! ๏ linkedin.com/in/jeremycoates @magma_digital
  • 3. What is Codeception? A new powerful testing framework written in PHP, using Symfony2 Components, PHPUnit & Behat's Mink drivers (for wire protocol).! ! In the words of Codeception: Codeception is a BDD-styled PHP testing framework, whose only requirements are basic knowledge of PHP and the theory of automated testing. Codeception is kept as simple as possible for any kind of users. “ @magma_digital
  • 4. The Pitch Codeception:! ๏ Is easy to read / write / debug! ๏ Enables users with different roles to share common test language! ๏ Integrates with IDE code completion! ๏ Describes actions from a users point of view! ๏ Makes testing fun! @magma_digital
  • 5. What Does Codeception Do? You can test your applications, through multiple approaches:! ๏ Unit Tests (CodeGuy)! ๏ Functional Tests (TestGuy)! ๏ Acceptance Tests (WebGuy)! ๏ API Tests (ApiGuy)! ๏ Database Repopulation! Focus on Acceptance / Functional / Db @magma_digital 5 of 46
  • 6. Codeception Features (1) ๏ Multiple backends, easily changed in configuration! - Selenium, PhpBrowser, ZombieJS, PhantomJS*! ๏ Elements matched by Name, CSS, XPath! ๏ Data Cleanup after each run! ๏ Integrate with! - Symfony2, Zend Framework, Kohana, Yii, Laravel etc.! ๏ Continuous Integration systems like Jenkins or Bamboo @magma_digital
  • 7. Codeception Features (2) ๏ Natively executes PHPUnit tests! ๏ BDD-style scenario-driven tests! ๏ WebServices testing via REST, SOAP, XML-RPC! ๏ Generates HTML, XML, TAP, JSON reports! ๏ Simple Fixtures (known test data)! ๏ Database and Doctrine ORM helpers! ๏ CodeCoverage and Remote CodeCoverage @magma_digital
  • 8. Codeception Modules Modules extend the connectivity and therefore the things you can test with Codeception - additional methods for the $I Guy.! ! This is the current released set of modules, however new ones are being created with regularity e.g. ZF2 has recently been accepted ! as a module. @magma_digital
  • 9. What You Need to Know? Codeception utilises:! ๏ Symfony2 Components! ๏ PHPUnit! ๏ Behat's Mink drivers! ๏ Facebook webdriver! - for wire protocol! ! License:! ๏ MIT Licence @magma_digital Requires:! ๏ PHP 5.3+! ๏ CURL enabled! ! Developed by:! ๏ Michael Bodnarchuk @davert, based in Kiev, Ukraine runs outsourcing company Codegyre! ๏ Sponsored by 2amigOS! Since June 2013 (sponsors of Yii)
  • 10. Acceptance vs Functional vs Unit ๏ Browser emulation (Mink / Selenium / WebDriver)! ๏ Test any website! ๏ Test JavaScript & Ajax! ๏ Readable by humans (managers!)! ๏ Stability against code changes! ๏ Some false positives! ๏ SLOW! @magma_digital
  • 11. Acceptance vs Functional vs Unit ๏ Emulate web request & submit to application! ๏ Assert against response & query internal values! ๏ Framework based! ๏ Still readable by humans! ๏ Can't test JS/Ajax! ๏ Risk of false positives! ๏ Less slow @magma_digital
  • 12. Acceptance vs Functional vs Unit ๏ Runs on top of PHPUnit with bells & whistles! ๏ Tests feel simpler / cleaner! ๏ Not readable by humans ! ๏ Test application core! ๏ Isolated units ! ๏ Fastest! @magma_digital
  • 13. Codeception: Howto? ๏ bootstrap - set up test folder & base files! ๏ configure - to suit how you want to test! ๏ build - when add suite, automatic otherwise! ๏ generate:cept generate:test generate:phpunit generate:scenarios <suite> <test>! ๏ Write test - unfortunately you still have to do this bit!! ๏ run --steps --debug --silent --report --coverage --html --xml --json [<suite>] [<test>] @magma_digital
  • 14. Naysayers? ๏I don't need no stinking tests!! ๏I already use PHPUnit, why switch?! ๏I Behat now, why write things only once?! ๏I already use Selenium (with PHPUnit), why change?! ๏What's all this Guy business about? @magma_digital Don’t need it, not listening !
  • 15. Installation - Phar ๏Phar file installation is the preferred method! ! ๏Grab Codeception phar executable:! - wget http://codeception.com/codecept.phar -O codecept.phar! ! ๏Prepare tests directory and configs:! - php codecept.phar bootstrap @magma_digital
  • 16. Installation - Composer ๏Install Composer as normal:! - curl -sS https://getcomposer.org/installer | php! ! ๏Add Codeception to composer.json:! - php composer.phar require “codeception/codeception:@stable”! ! ๏Run Codeception from vendor path! - vendor/bin/codecept bootstrap @magma_digital
  • 17. Advanced Usage (1) ๏ Helpers - simply add Actions to your Guys! ๏ Groups - put tests into groups and provide hooks for them! - $scenario->group(<groupname>) $scenario- >group(array(<groupname>, <groupname>))! - run --group <groupname> (or -g)! ๏ Console - interactive console, try commands without tests! ๏ Config Reconfigure - dynamically change module config (from helpers ->_reconfigure(array(‘browser’ => ‘chrome’)); )! ๏ Stubs - mocking code Stub::makeEmpty (see also AspectMock framework) @magma_digital
  • 18. Advanced Usage (2) ๏ PageObjects - new classes for defining UI maps! ๏ StepObjects - common actions used in different tests! ๏ Events - fire at various points during test execution! ๏ Extension & Group classes that can hook into events and produce their own. Extend anything! ๏ Conditional Asserts - don’t stop a test if assert fails! - each ‘see’ method has ‘canSee’ equivalent! - each ‘dontSee’ method has ‘cantSee’ equivalent! ๏ Comments - $I[‘describe everything in comments and then automate’]; @magma_digital
  • 19. Roadmap ๏ Codeception 2.0 (June ’14):! - Removed Mink - replace with WebDriver module (Facebook API)! - Tests not executed twice as previous! - PHP 5.4+! - No Guys, new folder structure _log -> _output, _helpers -> _support! - Remove: Selenium/2, ZombieJS -> WebDriver (Facebook API)! - Updated dependencies - e.g. Guzzle 4! - No BC breaks? :) @magma_digital
  • 20. Codeception: Add-ons ๏ http://codeception.com/addons - useful add-ons, not core to Codeception! ๏WebCeption (Application): web front end for Codeception! ๏ VisualCeption (Module): visual regression tests! ๏ DrushDb (Extension): populate/clean up Drupal site with drush / sql-sync during test runs! ๏ RemoteDebug (Extension): code coverage from remote sites @magma_digital
  • 21. Resources ๏ Codeception: http://codeception.com/! ๏ Codeception Github: http://github.com/codeception/Codeception/! ๏ Composer: http://getcomposer.org/! ๏ Selenium WebDriver: http://docs.seleniumhq.org/download/! ๏ ChromeDriver: http://code.google.com/p/chromedriver/! ๏ GhostDriver: https://github.com/detro/ghostdriver (now part of PhantomJS)! ๏ PhantomJS: http://phantomjs.org/! ๏ Netbeans IDE: http://netbeans.org/downloads/! ๏WebDriver Wire Protocol:! - http://code.google.com/p/selenium/wiki/JsonWireProtocol @magma_digital
  • 22. Now Get Testing... https://joind.in/11764 ๏ Jeremy Coates, MD at Magma Digital Ltd! ๏ Founder of PHPNW! ๏ Manchester Digital Association Council Member! ๏ @phpcodemonkey! ๏ linkedin.com/in/jeremycoates @magma_digital Download Slides Here ! http://bit.ly/test_with_codeception