Testing with Codeception

Post on 08-May-2015

12.405 views 6 download

description

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

Transcript of Testing with Codeception

@magma_digital

RefactorGreen

Testing with CodeceptionJeremy Coates / @phpcodemonkey

Red

@magma_digital

Who am I?

๏ Jeremy Coates, CEO at Magma Digital Ltd๏ Founder of PHPNW๏ Lancashire Digital CIC founding director๏ @phpcodemonkey๏ linkedin.com/in/jeremycoates

@magma_digital

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

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!

The Pitch

@magma_digital

You can test your applications, through multiple approaches:

๏ Unit Tests

๏ Functional Tests

๏ Acceptance Tests

๏ API Tests

๏ Database Repopulation

Focus on Acceptance / Functional / Db

What Does Codeception Do?

5 of 46

@magma_digital

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

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

Codeception Modules

Modules extend the connectivity and therefore the things you can test with Codeception - additional methods for the $I object.

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

Codeception utilises:

๏ Symfony2 Components

๏ PHPUnit

๏ Behat's Mink drivers

๏ Facebook webdriver

- for wire protocol

License:

๏ MIT Licence

What You Need to Know?Requires:

๏ PHP 5.3+ (v1.x)

๏ CURL enabled

Developed by:

๏ Michael Bodnarchuk @davert, based in Kiev, Ukraine runs outsourcing company Codegyre

๏ Sponsored by 2amigOS! Since June 2013 (sponsors of Yii)

@magma_digital

๏ Browser emulation (Mink / Selenium / WebDriver)

๏ Test any website

๏ Test JavaScript & Ajax

๏ Readable by humans (managers!)

๏ Stability against code changes

๏ Some false positives

๏ SLOW!

Acceptance vs Functional vs Unit

@magma_digital

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

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

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

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?

Don’t need it, not listening !

@magma_digital

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

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

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

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

๏ 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? :)

Roadmap (Achieved)

@magma_digital

๏ Codeception 2.1 (April ’15):- PSR-4 Support - test/_support - new location for all support code- *Tester classes generated once - traits for common code, customise :)- Dependency Injection - page/step objects, modules -> helpers- Simplified config - specify helpers in config- Environment Matrix - tests/_envs folder. codecept run —env dev,chrome- Module Parts - pic’n’mix partial loading of actions- WebDriver - session snapshots - skip logging in every time!- No major BC breaks? :)

Roadmap (Next)

@magma_digital

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

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

๏ Jeremy Coates, CEO at Magma Digital Ltd๏ Founder of PHPNW๏ Lancashire Digital CIC founding director๏ @phpcodemonkey

๏ linkedin.com/in/jeremycoates

Now Get Testing...

DownloadSlides Here!http://bit.ly/test_with_codeception