49
Globalcode – Open4education PHP Trail – Tests and PHP Continuous Integration Environment for Agile Development Bruno Yukio Tanoue Web Developer – UOL BoaCompra

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

Embed Size (px)

Citation preview

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

Globalcode – Open4education

PHP Trail – Tests and PHP Continuous Integration Environment for Agile

DevelopmentBruno Yukio Tanoue

Web Developer – UOL BoaCompra

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

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: [email protected]: https://br.linkedin.com/in/brunotanoue/en

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

Globalcode – Open4education

Schedule

Tests PyramidSolitary Unit Test x Sociable Unit Test Optimizations for Running TestsPHP Continuous Integration Environment

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

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

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

Globalcode – Open4education

Tests Pyramid

http://martinfowler.com/bliki/TestPyramid.html

T

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

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.

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

Globalcode – Open4education

Solitary Unit Test x Sociable Unit Test

Solitary Unit Test

Controller Model Database

Controller Model(Mock)

Unit

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

Globalcode – Open4education

Solitary Unit Test x Sociable Unit Test

Solitary Unit TestAdvantages:

Quick execution.Faster error location.Exception simulation.

Disadvantages:Possible outdated mock.False positive at tests.Possible bug at production.

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

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.

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

Globalcode – Open4education

Unit

Solitary Unit Test x Sociable Unit Test

Sociable Unit Test

Controller Model Database

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

Globalcode – Open4education

Solitary Unit Test x Sociable Unit Test

Sociable Unit TestAdvantages:

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

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

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!!!!

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

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 )

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

Globalcode – Open4education

Optimizations for Running Tests

Environment OptimizationsProblems 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.

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

Globalcode – Open4education

Optimizations for Running Tests

Environment Optimizations

STAGING DATABASE LOCAL TEST DATABASE

BEFORE AFTER

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

Globalcode – Open4education

Optimizations for Running Tests

Code Optimizations – Unit Tests@dataProvider : Creating a data provider to produce entries for a test.

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

Globalcode – Open4education

Optimizations for Running Tests

Without @dataProvider

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

Globalcode – Open4education

Optimizations for Running Tests

With @dataProvider

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

Globalcode – Open4education

Optimizations for Running Tests

Code Optimizations – UI TestssetUp e tearDown

At the suite execution, it prevents multiple browsers stay open if several tests fail.

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

Globalcode – Open4education

Optimizations for Running Tests

Code Optimizations – UI TestssetUpBeforeClass e tearDownAfterClass

What if instead, the browser was opened and closed once at each test class?

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

Globalcode – Open4education

Optimizations for Running Tests

Code Optimizations – UI TestsSurefire Plugin (Running tests in parallel)

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

Globalcode – Open4education

Optimizations for Running Tests

Code Optimizations – UI TestsResults – Build #73 (Using setUp and tearDown)

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

Globalcode – Open4education

Optimizations for Running Tests

Code Optimizations – UI TestsResults – Build #75 (Using setUpBeforeClass and tearDownAfterClass)

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

Globalcode – Open4education

Optimizations for Running Tests

Code Optimizations – UI TestsResults – Build #75 (Using setUpBeforeClass and tearDownAfterClass)

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

Globalcode – Open4education

Optimizations for Running Tests

Code Optimizations – UI TestsResults – Build #76 (parallel tests)

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

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.

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

Globalcode – Open4education

PHP Continuous Integration Environment

Monitoring

Central repository

with version control

CI SERVER

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

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

BuildingPackage

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

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.

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

Globalcode – Open4education

PHP Continuous Integration Environment

How does create a basic continuous integration (CI) server for PHP?

PHP/PHPUnitRunning 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.

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

Globalcode – Open4education

PHP Continuous Integration Environment

Thanks champs!!! So now I’ll install everything manually!!!!

“IT’S RIGHT!!”! :P

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

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.

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

Globalcode – Open4education

PHP Continuous Integration Environment

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

Globalcode – Open4education

PHP Continuous Integration Environment

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

Globalcode – Open4education

PHP Continuous Integration Environment

Other useful plugins:Clover PHP

Code coverage chart for each building.Warnings when code coverage decreases.

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

Globalcode – Open4education

PHP Continuous Integration Environment

Other useful plugins:Clover PHP

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

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.

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

Globalcode – Open4education

PHP Continuous Integration Environment

Other useful plugins:HTML Publisher

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

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

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

Globalcode – Open4education

PHP Continuous Integration Environment

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

Globalcode – Open4education

PHP Continuous Integration Environment

Creating and Running a PHP job in Jenkins:Step 1: Setting up the management of source code.

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

Globalcode – Open4education

PHP Continuous Integration Environment

Creating and Running a PHP job in Jenkins:Step 2: Setting up the repository verification method (trigger).

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

Globalcode – Open4education

PHP Continuous Integration Environment

Creating and Running a PHP job in Jenkins:Step 3: Setting up the tests.

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

Globalcode – Open4education

PHP Continuous Integration Environment

Creating and Running a PHP job in Jenkins:Step 4: Setting up the building packages.

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

Globalcode – Open4education

PHP Continuous Integration Environment

Creating and Running a PHP job in Jenkins:Step 5: Saving and executing!!!!

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

Globalcode – Open4education

PHP Continuous Integration Environment

Execution Logs:

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

Globalcode – Open4education

PHP Continuous Integration Environment

Execution Logs:

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

Globalcode – Open4education

PHP Continuous Integration Environment

Execution Logs:

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

Globalcode – Open4education

THE END

Email: [email protected]

Any doubt?Any doubt?