Codeception: introduction to php testing

20
Thomas Dutrion / @tdutrion - September 2014 Codeception Introduction to PHP testing

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

Page 1: Codeception: introduction to php testing

Thomas Dutrion / @tdutrion - September 2014

CodeceptionIntroduction to PHP testing

Page 2: Codeception: introduction to php testing

About me

@tdutrion / @engineor / [email protected]

● Founder and developer/architect at Engineor

● Working with PHP since 2003

● Trying to work properly...

Page 3: Codeception: introduction to php testing

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

Page 4: Codeception: introduction to php testing

● Unit testing

● Integration testing

● Functional testing

● Acceptance testing

● Mutation testing

● Monkey testing

● ...

Codeception

What can we test?

And you, how are you testing?

Page 5: Codeception: introduction to php 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

Page 6: Codeception: introduction to php testing

● Test modules as groups

● Simulate input

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

Codeception

Integration testing

Page 7: Codeception: introduction to php 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

Page 8: Codeception: introduction to php testing

Codeception

One tool: Codeception

Page 9: Codeception: introduction to php testing

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

Page 10: Codeception: introduction to php testing

Codeception

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

Page 11: Codeception: introduction to php testing

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

Page 12: Codeception: introduction to php testing

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

Page 13: Codeception: introduction to php testing

$ 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)

Page 14: Codeception: introduction to php testing

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)

Page 15: Codeception: introduction to php testing

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

Page 16: Codeception: introduction to php testing

Demonstration: Firefox run

Page 17: Codeception: introduction to php testing

● 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

Page 18: Codeception: introduction to php testing

Codeception

Demonstration: headless run

Page 19: Codeception: introduction to php testing

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

Page 20: Codeception: introduction to php testing

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