4. MODERN PHP:
INDUSTRIALISATION
PHP is mature (object oriented, namespaces...).
PHP has a large community.
PHP has a large ecosystem.
WHY LARGE CORPORATION AREN'T
USING IT MORE OFTEN?
6. WHAT CAN WE TEST?
Unit testing
Integration testing
Functional testing
Acceptance testing
Mutation testing
...
7. WHAT CAN WE CHARGE FOR?
Unit testing (sometimes)
Integration testing (occasionnaly)
Functional testing (occasionnaly)
Acceptance testing (sometimes)
Mutation testing (rarely)
8. UNIT TESTING
Small units of isolated code (SOLID)
Takes time to write and update
Perfect for critical processes in your application
9. INTEGRATION TESTING
Test larger units of code together
Does not give specific details to find a bug
Less expensive (less tests)
10. ACCEPTANCE TESTING
High level testing (as a user)
Client point of view
Less code, easier to implement
Only test final result
Can also be used in business to prove the project
completion
12. GENERAL INTRODUCTION
HISTORY
Started in 2011
First stable January 2012
Currently 2.1.5
4677 commits yesterday night (4267 in October*)
322 contributors yesterday (295 in October*)
*same talk in October last year for DundeePHP
13. GENERAL INTRODUCTION
PROBLEM SOLVED
Bridge between different testing types
No other languages required (for php developers)
Extensible
Covers all the major PHP framework
15. PHP 5.4 MINIMUM (USE AT LEAST 5.6 ANYWAYS!)
PHP supported versions
16. YOU ALREADY KNOW IT!
OR NOT, BUT YOU SHOULD HAVE A LOOK AT THESE
TOOLS
Based on recommended and proven tools
/ (optional)
...
PHPUnit
Symfony browserkit
Selenium PhantomJS
17. YOU ALREADY KNOW IT!
Test suites are written in PHP.
You can test websites created in any language (as you are
only testing the result)
19. FOLLOW THE QUICKSTART!
1. Install codeception (prefer using composer, globally or
in dev only)
2. Bootstrap (directories and files structure, basic
configuration)
3. Generate acceptance testing
4. Write tests
5. And run!
http://codeception.com/quickstart
20. ...
INSTALL (WITH COMPOSER)
$ composer require "codeception/codeception:*"
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing symfony/polyfill-mbstring (v1.0.1)
Loading from cache
- Installing phpunit/phpunit (4.8.21)
Loading from cache
- Installing codeception/codeception (2.1.5)
Downloading: 100%
Writing lock file
Generating autoload files
21. DIRECTORY CONTENT
$ tree -I vendor
.
├── composer.json
└── composer.lock
Option -I to remove the vendor folder from the display as
it contains only third party libraries.
22. BOOTSTRAP
$ php vendor/bin/codecept bootstrap
Initializing Codeception in /presentations/2016-01-13-Codeception-Techmeetup-
File codeception.yml created <- global configuration
tests/unit created <- unit tests
tests/unit.suite.yml written <- unit tests suite configuration
tests/functional created <- functional tests
tests/functional.suite.yml written <- functional tests suite configuration
tests/acceptance created <- acceptance tests
tests/acceptance.suite.yml written <- acceptance tests suite configuration
---
tests/_bootstrap.php written <- global bootstrap file
Building initial Tester classes
Building Actor classes for suites: acceptance, functional, unit
-> AcceptanceTesterActions.php generated successfully. 0 methods added
AcceptanceTester includes modules: PhpBrowser, HelperAcceptance
23. DIRECTORY CONTENT
$ tree -I vendor
.
├── codeception.yml
├── composer.json
├── composer.lock
└── tests
├── _bootstrap.php
├── _data
│ └── dump.sql
├── _envs
├── _output
├── _support
│ ├── AcceptanceTester.php
│ ├── FunctionalTester.php
│ ├── Helper
│ │ ├── Acceptance.php
│ │ ├── Functional.php
Option -I to remove the vendor folder from the display as
it contains only third party libraries.
24. TEST GENERATION
$ vendor/bin/codecept generate:cept acceptance Welcome
Test was created in /presentations/2016-01-13-Codeception-Techmeetup-Edinburg
Generates tests/acceptance/WelcomeCept.php:
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('perform actions and see result');
25. WRITE TESTS
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that Edinburgh is listed on the homepage');
$I->amOnPage('/');
$I->see('Edinburgh');
tests/acceptance/WelcomeCept.php
26. UPDATE CONFIGURATION
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
class_name: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://techmeetup.co.uk/
- HelperAcceptance
tests/acceptance.suite.yml
27. BASIC EXAMPLE RESULTS (PASS)
$ vendor/bin/codecept run
Codeception PHP Testing Framework v2.1.5
Powered by PHPUnit 4.8.21 by Sebastian Bergmann and contributors.
Acceptance Tests (1) --------------------------------------------
Ensure that frontpage works (WelcomeCept) Ok
-----------------------------------------------------------------
Functional Tests (0) --------------------------------------------
Unit Tests (0) --------------------------------------------------
Time: 630 ms, Memory: 10.00Mb
OK (1 test, 1 assertion)
28. ADD TEST
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that Edinburgh is listed on the homepage');
$I->amOnPage('/');
$I->see('Edinburgh');
$I->see('This does not appear on the page');
tests/acceptance/WelcomeCept.php
29. BASIC EXAMPLE RESULTS (FAIL)
$ vendor/bin/codecept run
Codeception PHP Testing Framework v2.1.5
Powered by PHPUnit 4.8.21 by Sebastian Bergmann and contributors.
Acceptance Tests (1) ---------------------------------------------
Ensure that frontpage works (WelcomeCept) Fail
------------------------------------------------------------------
Functional Tests (0) ---------------------------------------------
Unit Tests (0) ---------------------------------------------------
Time: 605 ms, Memory: 10.00Mb
30. BASIC EXAMPLE RESULTS (FAIL)
There was 1 failure:
---------
1) Failed to ensure that frontpage works in WelcomeCept (tests/acceptance/Wel
Step I see "This does not appear on the page"
Fail Failed asserting that /
-->
TechMeetup - Home
Tech Meetup
About
Videos
Blog
Calendar
TechMeetup is a monthly excuse for developers and the tech communit
[Content too long to display. See complete response in '_output' directory]
--> contains "this does not appear on the page".
31. BASIC EXAMPLE RESULTS (FAIL)
Scenario Steps:
3. $I->see("This does not appear on the page") at tests/acceptance/WelcomeCe
2. $I->see("Edinburgh") at tests/acceptance/WelcomeCept.php:5
1. $I->amOnPage("/") at tests/acceptance/WelcomeCept.php:4
FAILURES!
Tests: 1, Assertions: 2, Failures: 1.
32. CHANGE TESTS
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that Edinburgh is listed on the homepage');
$I->amOnPage('/');
$I->see('Edinburgh');
$I->click('Edinburgh');
$I->amOnPage('/edinburgh.html');
$I->see('2nd Wed of month');
tests/acceptance/WelcomeCept.php
33. RESULTS
$ vendor/bin/codecept run
Codeception PHP Testing Framework v2.1.5
Powered by PHPUnit 4.8.21 by Sebastian Bergmann and contributors.
Acceptance Tests (1) --------------------------------------------
Ensure that frontpage works (WelcomeCept) Ok
-----------------------------------------------------------------
Functional Tests (0) --------------------------------------------
Unit Tests (0) --------------------------------------------------
Time: 834 ms, Memory: 10.00Mb
OK (1 test, 2 assertions)
35. PROBLEM:
Since 2005, AJAX is everywhere (XmlHttpRequest)
New architecture: MVVM javascript in front,
PHP/node… in back
Testing problem: PHPBrowser / Curl can not read
javascript modifications.
36. SOLUTION 1:
USE SELENIUM DRIVER AND DELAYS
Simple configuration (see the documentation).
Looks good when showing off to your client!
composer require --dev "netwing/selenium-server-standalone:^2.46"
37. SOLUTION 1:
USE SELENIUM DRIVER AND DELAYS
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
class_name: AcceptanceTester
modules:
enabled:
- WebDriver
- HelperAcceptance
config:
WebDriver:
url: 'https://phpmentoring.org'
browser: 'firefox'
window_size: 1024x768
38. SOLUTION 1:
USE SELENIUM DRIVER AND DELAYS
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that tdutrion is displayed as a mentor');
$I->amOnPage('/mentors');
$I->wait(1);
$I->see('Thomas Dutrion');
39. SOLUTION 1:
USE SELENIUM DRIVER AND DELAYS
java -jar vendor/bin/selenium-server-standalone-2.46.0.jar
vendor/bin/codecept run
40. SOLUTION 2:
USE SOMETHING MORE APPROPRIATE?
Google recommends , but other exist
( , ).
Karma
Nightwatch.js Jasmine
41. PROBLEM 2:
I can not use Java and Firefox on my continuous
integration server!
(Who would do that?)
44. THANKS FOR HAVING ME!
Special thanks to / reading recommendation:
Jeremy Coates ( ) for
PHPNW ( ), great PHP group that made me discover
Codeception
All of you for your patience and supporting me!
Techmeetup ( ) to let me talk here
@phpcodemonkey Testing with codeception
@phpnw
@techmeetup
Please rate and comment this talk on SlideShare: http://goo.gl/068L56