Globalcode – Open4education
PHP Trail – Tests and PHP Continuous
Integration Environment for Agile
Development
Bruno Yukio Tanoue
Web Developer – UOL BoaCompra
Globalcode – Open4education
Bruno Tanoue
Web Developer @ UOL BoaCompra ( 3 years and
5 months)
BoaCompra Checkout and Payment Gateway.
BoaCompra Financial System.
Bachelor’s degree in Computer Science from UEM
(State University of Maringá)
Email: brunotanoue@hotmail.com
LinkedIn: https://br.linkedin.com/in/brunotanoue/en
Globalcode – Open4education
Schedule
Tests Pyramid
Solitary Unit Test x Sociable Unit Test
Optimizations for Running Tests
PHP Continuous Integration Environment
Globalcode – Open4education
Tests Pyramid
Goal: To provide an adequate tests proportion to
run and a quick feedback.
Unit Tests: To verify the quality at small code snippets
(units).
e.g., PHPUnit
Integration Tests: To test the integration of the tested
units.
e.g., PHPUnit + DBUnit
UI Tests (User Interface Tests): To validate flows and
information displayed at the user level.
e.g., Selenium Webdriver
Globalcode – Open4education
Tests Pyramid
http://martinfowler.com/bliki/TestPyramid.html
T
Globalcode – Open4education
Solitary Unit Test x
Sociable Unit Test
What is a unit test?
Concept more used and defended:
Solitary Unit Test: Methods are tested in isolation from
other internal methods and external communications
using mocks.
Mock: It’s a change of a real structure to a simulated
structure for running tests.
Globalcode – Open4education
Solitary Unit Test x
Sociable Unit Test
Solitary Unit Test
Controller Model Database
Controller Model(Mock)
Unit
Globalcode – Open4education
Solitary Unit Test x
Sociable Unit Test
Solitary Unit Test
Advantages:
Quick execution.
Faster error location.
Exception simulation.
Disadvantages:
Possible outdated mock.
False positive at tests.
Possible bug at production.
Globalcode – Open4education
Solitary Unit Test x
Sociable Unit Test
What is a unit test?
Less known concept:
Sociable Unit Test : Methods are tested in collaboration
from other internal methods and external
communications.
What is unit in this case?
It depends on the depth of your test.
Globalcode – Open4education
Unit
Solitary Unit Test x
Sociable Unit Test
Sociable Unit Test
Controller Model Database
Globalcode – Open4education
Solitary Unit Test x
Sociable Unit Test
Sociable Unit Test
Advantages:
Running tests with the real structure.
More thoroughly tested scenario (units collaboration).
Disadvantages:
Slower runtime.
High dependence among components or systems.
False negative at tests ( e.g., external communications ).
Globalcode – Open4education
Solitary Unit Test x
Sociable Unit Test
What kind of unit test is best?
It’s necessary to review each case depending on
the need and use one of two test types.
What about the tests pyramid with the sociable unit
tests?
http://martinfowler.com/bliki/UnitTest.html
TOO CONFUSING!!!!
Globalcode – Open4education
Optimizations for Running
Tests
What is important for running tests?
It must pass confidence that code is reliable.
It must be easy to understand.
The suite execution must be as fast as possible, so that
it runs as soon as possible and many times as
necessary. (e.g., every commit )
Globalcode – Open4education
Optimizations for Running
Tests
Environment Optimizations
Problems that can delay the running tests:
Network concurrence.
Database concurrence.
Possible solution: To centralize the testing environment
on a single machine, either virtual or real with its own
database for testing.
Globalcode – Open4education
Optimizations for Running
Tests
Environment Optimizations
STAGING DATABASE LOCAL TEST DATABASE
BEFORE AFTER
Globalcode – Open4education
Optimizations for Running
Tests
Code Optimizations – Unit Tests
@dataProvider : Creating a data provider to produce
entries for a test.
Globalcode – Open4education
Optimizations for Running
Tests
Without @dataProvider
Globalcode – Open4education
Optimizations for Running
Tests
With @dataProvider
Globalcode – Open4education
Optimizations for Running
Tests
Code Optimizations – UI Tests
setUp e tearDown
At the suite execution, it prevents multiple browsers stay open if
several tests fail.
Globalcode – Open4education
Optimizations for Running
Tests
Code Optimizations – UI Tests
setUpBeforeClass e tearDownAfterClass
What if instead, the browser was opened and closed once at
each test class?
Globalcode – Open4education
Optimizations for Running
Tests
Code Optimizations – UI Tests
Surefire Plugin (Running tests in parallel)
Globalcode – Open4education
Optimizations for Running
Tests
Code Optimizations – UI Tests
Results – Build #73 (Using setUp and tearDown)
Globalcode – Open4education
Optimizations for Running
Tests
Code Optimizations – UI Tests
Results – Build #75 (Using setUpBeforeClass and
tearDownAfterClass)
Globalcode – Open4education
Optimizations for Running
Tests
Code Optimizations – UI Tests
Results – Build #75 (Using setUpBeforeClass and
tearDownAfterClass)
Globalcode – Open4education
Optimizations for Running
Tests
Code Optimizations – UI Tests
Results – Build #76 (parallel tests)
Globalcode – Open4education
PHP Continuous
Integration Environment
What is a continuous integration environment?
Continuous Integration
Agile process.
It defends the continuous code delivery in a central repository in
a short time.
The code to be integrated can’t produce defects at the existing
code. (tests)
The continuous integration environment aims to
automate tasks of the continuous integration process.
Automation is important to decrease the errors of human
nature.
Globalcode – Open4education
PHP Continuous
Integration Environment
Monitoring
Central
repository
with
version
control
CI
SERVER
Globalcode – Open4education
PHP Continuous
Integration Environment
How does create a basic continuous integration
(CI) server for PHP?
Verification
and
Updating of
Repository
Copy
Running
Tests
Building
Package
Globalcode – Open4education
PHP Continuous
Integration Environment
How does create a basic continuous integration
(CI) server for PHP?
CI Tool (Jenkins, Hudson, etc..)
It’s the heart of continuous integration server, where whole code
integration process, tests and building packages is done.
Versioning Control (GIT, SVN, etc...)
The CI server will verify the central repository from time to time
checking if there was a change. If there is a change, a building
will be triggered and the local copy is updated.
Globalcode – Open4education
PHP Continuous
Integration Environment
How does create a basic continuous integration
(CI) server for PHP?
PHP/PHPUnit
Running tests to validate the integrated code.
Web Server (Apache) and Database (MySQL, SQLite,
etc...)
Support for sociable unit tests, integration tests and UI tests.
Building Tool (Maven, Ant, etc...)
Building packages.
Globalcode – Open4education
PHP Continuous
Integration Environment
Thanks champs!!! So now I’ll install everything
manually!!!!
“IT’S RIGHT!!”! :P
Globalcode – Open4education
PHP Continuous
Integration Environment
Jenkins can manage the installation of most
components through PLUGINS.
It can manage multiple component versions.
Easy installation, removal or upgrade.
Globalcode – Open4education
PHP Continuous
Integration Environment
Globalcode – Open4education
PHP Continuous
Integration Environment
Globalcode – Open4education
PHP Continuous
Integration Environment
Other useful plugins:
Clover PHP
Code coverage chart for each building.
Warnings when code coverage decreases.
Globalcode – Open4education
PHP Continuous
Integration Environment
Other useful plugins:
Clover PHP
Globalcode – Open4education
PHP Continuous
Integration Environment
Other useful plugins:
HTML Publisher
PHPUnit Log: It provides code coverage charts of each folder,
class or method.
Covered and discovered lines information and dead code.
Globalcode – Open4education
PHP Continuous
Integration Environment
Other useful plugins:
HTML Publisher
Globalcode – Open4education
PHP Continuous
Integration Environment
The continuous integration process within Jenkins
is represented by Job.
A job can be configured in a very varied way, could
represent a step in the continuous integration, like whole
continuous integration process.
It’s possible to change the step execution order within a
job through a simple “drag and drop”.
Globalcode – Open4education
PHP Continuous
Integration Environment
Globalcode – Open4education
PHP Continuous
Integration Environment
Creating and Running a PHP job in Jenkins:
Step 1: Setting up the management of source code.
Globalcode – Open4education
PHP Continuous
Integration Environment
Creating and Running a PHP job in Jenkins:
Step 2: Setting up the repository verification method
(trigger).
Globalcode – Open4education
PHP Continuous
Integration Environment
Creating and Running a PHP job in Jenkins:
Step 3: Setting up the tests.
Globalcode – Open4education
PHP Continuous
Integration Environment
Creating and Running a PHP job in Jenkins:
Step 4: Setting up the building packages.
Globalcode – Open4education
PHP Continuous
Integration Environment
Creating and Running a PHP job in Jenkins:
Step 5: Saving and executing!!!!
Globalcode – Open4education
PHP Continuous
Integration Environment
Execution Logs:
Globalcode – Open4education
PHP Continuous
Integration Environment
Execution Logs:
Globalcode – Open4education
PHP Continuous
Integration Environment
Execution Logs:
Globalcode – Open4education
THE END
Email: brunotanoue@hotmail.com
Any doubt?Any doubt?

[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Environment for Agile Development