Codeception: introduction to php testing

Post on 27-May-2015

851 views 5 download

Tags:

description

Demonstrating of how Codeception can help us, PHP developers, with the issues that affect testing of projects with dynamic frontend (angular, ember, etc.).

Transcript of Codeception: introduction to php testing

Thomas Dutrion / @tdutrion - September 2014

CodeceptionIntroduction to PHP testing

About me

@tdutrion / @engineor / thomas@engineor.com

● Founder and developer/architect at Engineor

● Working with PHP since 2003

● Trying to work properly...

PHP3: simple web pages

PHP4: object? are you kidding?

PHP5: mature language, with namespaces, real objects and so on…

What is missing to compete with other languages?

TESTABILITY

Codeception

PHP best practice: industrialisation

● Unit testing

● Integration testing

● Functional testing

● Acceptance testing

● Mutation testing

● Monkey testing

● ...

Codeception

What can we test?

And you, how are you testing?

● Small unit of code

● SOLID principle (single Responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion)

● Requires complete isolation

● Long to write, requires continuous updates, expensive

=> very good for critical parts of the application!

Codeception

Unit testing

● Test modules as groups

● Simulate input

● Less work than Unit testing (smaller number of tests)

Codeception

Integration testing

● Test final application appearance

● Reproduce complete application execution

● Client point of view

● Less code, final result only, help on legacy projects

=> Can be used to prove the completion of the project

Codeception

Acceptance testing

Codeception

One tool: Codeception

General introduction

Codeception

History:

● Started in 2011

● Stable release in January 2012

● Version 2.0.5 in August 2014!

● Currently 2725 commits

● 175 contributors...

Problem solved:

● Bridge between all testing types

● No other languages required

● Extensible

● Covers major frameworks

Codeception

PHP 5.4 minimum!!!! (you can do that, right!)

You already know it!

Based on recommended and proven tools

● PHPUnit

● Symfony browserkit

● Selenium / PhantomJS (optional)

● …

Test suite written in PHP => no need to learn another language.

Codeception

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!

Codeception

Acceptance testing 101

$ php vendor/bin/codecept run

Codeception PHP Testing Framework v2.0.5

Powered by PHPUnit 4.2.5 by Sebastian Bergmann.

Acceptance Tests (1) ------------------------------------------------------------------------------------------------------------

Trying to ensure that frontpage works (WelcomeCept) Ok

---------------------------------------------------------------------------------------------------------------------------------

Functional Tests (0) ---------------------------------------------------------------------

Unit Tests (0) ---------------------------------------------------------------------------

Time: 382 ms, Memory: 8.50Mb

OK (1 test, 1 assertion)

Codeception

Basic example results (pass)

Acceptance Tests (1) ------------------------------------------------------------------------------------------------------------

Trying to ensure that frontpage works (WelcomeCept) Fail

There was 1 failure:

1) Failed to ensure that frontpage works in WelcomeCept (/Users/thomas/NetBeansProjects/Codeception01/tests/acceptance/WelcomeCept.php)

Couldn't see "Not ok":

Failed asserting that /

→ Your HTML code here

[Content too long to display. See complete response in '_output' directory]

--> contains "not ok".

Scenario Steps:

2. I see "Not ok"

1. I am on page "/"

FAILURES!

Tests: 1, Assertions: 1, Failures: 1.

Codeception

Basic example results (fail)

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

=> Codeception can work with Selenium!

Codeception

What about sexy frontends?

Demonstration: Firefox run

● No Firefox and or java on your servers => no Selenium on CI

● CI usually run only simple bash scripts

● Need to run the test in command line, emulating an headless browser

=> Codeception PhantomJS driver

Codeception

Headless testing for Continuous Integration

Codeception

Demonstration: headless run

Workflow improvement

Codeception

● Use Docker containers or Vagrant virtual boxes to add development, testing

and production servers configuration to your VCS repository

● Add CI server triggered on commits, with email results

Questions?

Codeception: introduction to PHP testing, by Thomas Dutrion from Engineor for Glasgow PHP, September 2014

Special thanks to / reading recommendation:

● Jeremy Coates (@phpcodemonkey) for Testing with codeception

● PHPNW (@phpnw), great PHP group that made me discover Codeception

● All of you for your patience and supporting my French accent!

● Glasgow PHP (@glasgowphp) to let me talk here

Please rate and comment this talk on SlideShare: http://goo.gl/uJIh7G