acceptance testing in php with codeception - techmeetup edinburgh

44
ACCEPTANCE TESTING IN PHP WITH CODECEPTION TECHMEETUP EDINBURGH JANUARY 2016 By / - 13/01/2016 Thomas Dutrion @tdutrion

Upload: engineor

Post on 14-Apr-2017

831 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Acceptance testing in php with Codeception - Techmeetup Edinburgh

ACCEPTANCE TESTING IN PHPWITH

CODECEPTIONTECHMEETUP EDINBURGH JANUARY

2016

By / - 13/01/2016Thomas Dutrion @tdutrion

Page 2: Acceptance testing in php with Codeception - Techmeetup Edinburgh

By / - 13/01/2016Thomas Dutrion @tdutrion

ABOUT MEFounder & Developer / web architect at Working with PHP since 2003Doing my best to work properly!

Engineor

Page 3: Acceptance testing in php with Codeception - Techmeetup Edinburgh

SCOTLAND PHP

Aberdeen (1st Wednesday) Edinburgh (3rd Tuesday)

Glasgow (3rd Tuesday) Dundee (3rd Thursday)

https://www.scotlandphp.co.uk

Page 4: Acceptance testing in php with Codeception - Techmeetup Edinburgh

MODERN PHP:INDUSTRIALISATION

PHP is mature (object oriented, namespaces...).

PHP has a large community.

PHP has a large ecosystem.

WHY LARGE CORPORATION AREN'TUSING IT MORE OFTEN?

Page 5: Acceptance testing in php with Codeception - Techmeetup Edinburgh

TESTABILITY

Page 6: Acceptance testing in php with Codeception - Techmeetup Edinburgh

WHAT CAN WE TEST?Unit testingIntegration testingFunctional testingAcceptance testingMutation testing...

Page 7: Acceptance testing in php with Codeception - Techmeetup Edinburgh

WHAT CAN WE CHARGE FOR?Unit testing (sometimes)Integration testing (occasionnaly)Functional testing (occasionnaly)Acceptance testing (sometimes)Mutation testing (rarely)

Page 8: Acceptance testing in php with Codeception - Techmeetup Edinburgh

UNIT TESTINGSmall units of isolated code (SOLID)Takes time to write and update

Perfect for critical processes in your application

Page 9: Acceptance testing in php with Codeception - Techmeetup Edinburgh

INTEGRATION TESTINGTest larger units of code togetherDoes not give specific details to find a bugLess expensive (less tests)

Page 10: Acceptance testing in php with Codeception - Techmeetup Edinburgh

ACCEPTANCE TESTINGHigh level testing (as a user)Client point of viewLess code, easier to implementOnly test final result

Can also be used in business to prove the projectcompletion

Page 11: Acceptance testing in php with Codeception - Techmeetup Edinburgh

CODECEPTIONONE TOOL, MULTIPLE TESTING TYPES

Page 12: Acceptance testing in php with Codeception - Techmeetup Edinburgh

GENERAL INTRODUCTIONHISTORY

Started in 2011First stable January 2012Currently 2.1.54677 commits yesterday night (4267 in October*)322 contributors yesterday (295 in October*)

*same talk in October last year for DundeePHP

Page 13: Acceptance testing in php with Codeception - Techmeetup Edinburgh

GENERAL INTRODUCTIONPROBLEM SOLVED

Bridge between different testing typesNo other languages required (for php developers)ExtensibleCovers all the major PHP framework

Page 14: Acceptance testing in php with Codeception - Techmeetup Edinburgh

LET'S GET INTO IT...

Page 15: Acceptance testing in php with Codeception - Techmeetup Edinburgh

PHP 5.4 MINIMUM (USE AT LEAST 5.6 ANYWAYS!)

PHP supported versions

Page 16: Acceptance testing in php with Codeception - Techmeetup Edinburgh

YOU ALREADY KNOW IT!OR NOT, BUT YOU SHOULD HAVE A LOOK AT THESE

TOOLS

Based on recommended and proven tools

/ (optional)...

PHPUnitSymfony browserkitSelenium PhantomJS

Page 17: Acceptance testing in php with Codeception - Techmeetup Edinburgh

YOU ALREADY KNOW IT!Test suites are written in PHP.

You can test websites created in any language (as you areonly testing the result)

Page 18: Acceptance testing in php with Codeception - Techmeetup Edinburgh

ACCEPTANCE TESTING 101

Page 19: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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 testing4. Write tests5. And run!

http://codeception.com/quickstart

Page 20: Acceptance testing in php with Codeception - Techmeetup Edinburgh

...

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

Page 21: Acceptance testing in php with Codeception - Techmeetup Edinburgh

DIRECTORY CONTENT

$ tree -I vendor . ├── composer.json └── composer.lock

Option -I to remove the vendor folder from the display asit contains only third party libraries.

Page 22: Acceptance testing in php with Codeception - Techmeetup Edinburgh

BOOTSTRAP

$ php vendor/bin/codecept bootstrap Initializing Codeception in /presentations/2016-01-13-Codeception-Techmeetup-Edinburgh/demo

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 configurationtests/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, \Helper\Acceptance

Page 23: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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 asit contains only third party libraries.

Page 24: Acceptance testing in php with Codeception - Techmeetup Edinburgh

TEST GENERATION

$ vendor/bin/codecept generate:cept acceptance Welcome Test was created in /presentations/2016-01-13-Codeception-Techmeetup-Edinburgh/demo/tests/acceptance/WelcomeCept.php

Generates tests/acceptance/WelcomeCept.php:

<?php $I = new AcceptanceTester($scenario); $I->wantTo('perform actions and see result');

Page 25: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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

Page 26: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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/ - \Helper\Acceptance

tests/acceptance.suite.yml

Page 27: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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)

Page 28: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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

Page 29: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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

Page 30: Acceptance testing in php with Codeception - Techmeetup Edinburgh

BASIC EXAMPLE RESULTS (FAIL)There was 1 failure: --------- 1) Failed to ensure that frontpage works in WelcomeCept (tests/acceptance/WelcomeCept.php)

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".

Page 31: Acceptance testing in php with Codeception - Techmeetup Edinburgh

BASIC EXAMPLE RESULTS (FAIL)Scenario Steps:

3. $I->see("This does not appear on the page") at tests/acceptance/WelcomeCept.php: 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.

Page 32: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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

Page 33: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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)

Page 34: Acceptance testing in php with Codeception - Techmeetup Edinburgh

MODERN FRONTEND ISSUES

Page 35: Acceptance testing in php with Codeception - Techmeetup Edinburgh

PROBLEM:Since 2005, AJAX is everywhere (XmlHttpRequest)New architecture: MVVM javascript in front,PHP/node… in back

Testing problem: PHPBrowser / Curl can not readjavascript modifications.

Page 36: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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"

Page 37: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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 - \Helper\Acceptance config: WebDriver: url: 'https://phpmentoring.org' browser: 'firefox' window_size: 1024x768

Page 38: Acceptance testing in php with Codeception - Techmeetup Edinburgh

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');

Page 39: Acceptance testing in php with Codeception - Techmeetup Edinburgh

SOLUTION 1:USE SELENIUM DRIVER AND DELAYSjava -jar vendor/bin/selenium-server-standalone-2.46.0.jar

vendor/bin/codecept run

Page 40: Acceptance testing in php with Codeception - Techmeetup Edinburgh

SOLUTION 2:USE SOMETHING MORE APPROPRIATE?

Google recommends , but other exist( , ).

KarmaNightwatch.js Jasmine

Page 41: Acceptance testing in php with Codeception - Techmeetup Edinburgh

PROBLEM 2:I can not use Java and Firefox on my continuous

integration server!

(Who would do that?)

Page 42: Acceptance testing in php with Codeception - Techmeetup Edinburgh

SOLUTION:USE A HEADLESS BROWSER

(PHANTOMJS)

Just a configuration again, try at home!

Page 43: Acceptance testing in php with Codeception - Techmeetup Edinburgh

QUESTIONS?

Page 44: Acceptance testing in php with Codeception - Techmeetup Edinburgh

THANKS FOR HAVING ME!Special thanks to / reading recommendation:

Jeremy Coates ( ) for PHPNW ( ), great PHP group that made me discoverCodeceptionAll 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