acceptance testing in php with codeception - techmeetup edinburgh

Post on 14-Apr-2017

828 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

ACCEPTANCE TESTING IN PHPWITH

CODECEPTIONTECHMEETUP EDINBURGH JANUARY

2016

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

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

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

Engineor

SCOTLAND PHP

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

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

https://www.scotlandphp.co.uk

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?

TESTABILITY

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

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

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

Perfect for critical processes in your application

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

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

CODECEPTIONONE TOOL, MULTIPLE TESTING TYPES

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

GENERAL INTRODUCTIONPROBLEM SOLVED

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

LET'S GET INTO IT...

PHP 5.4 MINIMUM (USE AT LEAST 5.6 ANYWAYS!)

PHP supported versions

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

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

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

ACCEPTANCE TESTING 101

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

...

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

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.

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

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.

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

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

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

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)

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

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

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

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.

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

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)

MODERN FRONTEND ISSUES

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.

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"

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

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

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

vendor/bin/codecept run

SOLUTION 2:USE SOMETHING MORE APPROPRIATE?

Google recommends , but other exist( , ).

KarmaNightwatch.js Jasmine

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

integration server!

(Who would do that?)

SOLUTION:USE A HEADLESS BROWSER

(PHANTOMJS)

Just a configuration again, try at home!

QUESTIONS?

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

top related