Codeception Testing Framework -- English #phpkansai

38
Text Introducing Codeception (TDD)—>Test Driven Development

Transcript of Codeception Testing Framework -- English #phpkansai

Text

Introducing Codeception (TDD)—>Test Driven Development

AgendaPresentation of Speaker/ Company

Presentation of Codeception

Presentation of Tests Types

Demonstration & Hands-on

Best Practices

References

Q & A

@phpcon_kansai

Who the hell am I ?Batard Florent (28 years old)(Shenril)

Twitter: @shenril

Facebook: Florent Btd

Security Engineer (Ethical Hacker)

French Globe Trotter

Lived in France,UK,Switzerland,U.S,Japan

Fan of Japan and especially Kansai area and people

Style learning Japanese….

Who the hell am I ?Batard Florent (28 years old)(Shenril)

Twitter: @shenril

Facebook: Florent Btd

Security Engineer (Ethical Hacker)

French Globe Trotter

Lived in France,UK,Switzerland,U.S,Japan

Fan of Japan and especially Kansai area and people

Style learning Japanese….

Who the hell am I ?Batard Florent (28 years old)(Shenril)

Twitter: @shenril

Facebook: Florent Btd

Security Engineer (Ethical Hacker)

French Globe Trotter

Lived in France,UK,Switzerland,U.S,Japan

Fan of Japan and especially Kansai area and people

Style learning Japanese….

Who the hell am I ?Batard Florent (28 years old)(Shenril)

Twitter: @shenril

Facebook: Florent Btd

Security Engineer (Ethical Hacker)

French Globe Trotter

Lived in France,UK,Switzerland,U.S,Japan

Fan of Japan and especially Kansai area and people

Style learning Japanese….

Who the hell am I ?Batard Florent (28 years old)(Shenril)

Twitter: @shenril

Facebook: Florent Btd

Security Engineer (Ethical Hacker)

French Globe Trotter

Lived in France,UK,Switzerland,U.S,Japan

Fan of Japan and especially Kansai area and people

Style learning Japanese….

Who the hell am I ?Batard Florent (28 years old)(Shenril)

Twitter: @shenril

Facebook: Florent Btd

Security Engineer (Ethical Hacker)

French Globe Trotter

Lived in France,UK,Switzerland,U.S,Japan

Fan of Japan and especially Kansai area and people

Style learning Japanese….

Who the hell am I ?Batard Florent (28 years old)(Shenril)

Twitter: @shenril

Facebook: Florent Btd

Security Engineer (Ethical Hacker)

French Globe Trotter

Lived in France,UK,Switzerland,U.S,Japan

Fan of Japan and especially Kansai area and people

Style learning Japanese….

Why do i speak here?Worked for:

W3C in Sophia Antipolis

U.N (Security Consultant)

Private Banking

Television

Did conferences in Europe

Mainly Security (Youtube) (sorry french and english only)

Active Member of OWASP (Web Security Project)

Web Developer for the past 10 years

Interested in DevOps ,Servers, Infrastructure, Artificial Intelligence

Learning Animal

Why do i speak here?Worked for:

W3C in Sophia Antipolis

U.N (Security Consultant)

Private Banking

Television

Did conferences in Europe

Mainly Security (Youtube) (sorry french and english only)

Active Member of OWASP (Web Security Project)

Web Developer for the past 10 years

Interested in DevOps ,Servers, Infrastructure, Artificial Intelligence

Learning Animal

Why do i speak here?Worked for:

W3C in Sophia Antipolis

U.N (Security Consultant)

Private Banking

Television

Did conferences in Europe

Mainly Security (Youtube) (sorry french and english only)

Active Member of OWASP (Web Security Project)

Web Developer for the past 10 years

Interested in DevOps ,Servers, Infrastructure, Artificial Intelligence

Learning Animal

Why do i speak here?Worked for:

W3C in Sophia Antipolis

U.N (Security Consultant)

Private Banking

Television

Did conferences in Europe

Mainly Security (Youtube) (sorry french and english only)

Active Member of OWASP (Web Security Project)

Web Developer for the past 10 years

Interested in DevOps ,Servers, Infrastructure, Artificial Intelligence

Learning Animal

Who do I work for?Cook+biz

Awesome Team

Awesome Challenges

Who do I work for?Cook+biz

Awesome Team

Awesome Challenges

Who do I work for?Cook+biz

Awesome Team

Awesome Challenges

Why Testing?Why should I test my code ?

Reduce False assumptions -> If you rely on assumptions it will surely fail…

Validate that there is no regression

Make sure the code runs as expected

Test limit values (security,big data)

Automate repetitive tasks -> Good developers are lazy developers!!!

It makes debugging/correction so much easier

But…

Often developers finds it hard to write

Too long too run

Doesn’t correspond to business logic/behaviour/user scenario

CodeCeption(1)http://codeception.com/ (English)

Test Framework

Acceptance Tests

Functional Tests

Unit Tests

Easy

To read

To write

To debug

Natively handles PHPUnit -> standard

CodeCeption(2)BDD-style scenario-driver tests

WebServices tests (SOAP/REST/XML-RPC)

Generates reports(HTML/XML/JSON)

Laravel/Zend/Phalcon/Yii2/Symfony/Composer modules -> No excuse!

Integration with continuous deployment (Jenkins/Bamboo)

Can be used along with Selenium2 and PhantomJS for advanced tests

What does it look like?<?php $I = new AcceptanceTester($scenario); $I->wantTo('create wiki page'); $I->amOnPage('/'); $I->click('Pages'); $I->click('New'); $I->see('New Page'); $I->fillField('title', 'Hobbit'); $I->fillField('body', 'By Peter Jackson'); $I->click('Save'); $I->see('page created'); // notice generated $I->see('Hobbit','h1'); // head of page of is our title $I->seeInCurrentUrl('pages/hobbit'); $I->seeInDatabase('pages', array('title' => ‘Hobbit')); ?>

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

User Scenario

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

User Scenario

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

User Scenario

Developer Scenario

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

User Scenario

Developer Scenario

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

User Scenario

Developer Scenario

Function Testing

Acceptance Tests

Enable to do request and interact with the DOM

Use a fake browser to perform the queries

Can click and fill forms

Can be plugged with Selenium/PhantomJS to interact with JavaScript and screenshots

Slowest tests to run

Functional TestsSame as Acceptance test but does not use a browser

engine

Can be plugged with the framework to access internal functions and properties (Models, validation)

Use $_REQUEST, $_POST and $_GET

Faster

No DOM interaction (JS/Ajax/Events)

Unit TestsUsed to test functions Can be plugged with the framework to access internal functions and properties (Models, Validation) Fastest Difficult to use in MVC Not readable by Humans

public function testValidation() { $user = User::create(); $user->username = null; $this->assertFalse($user->validate(['username'])); $user->username = 'toolooooongnaaaaaaameeee'; $this->assertFalse($user->validate(['username'])); $user->username = 'davert'; $this->assertTrue($user->validate(['username'])); }

Install on all platformsMac OSX:

brew update && brew install homebrew/php/codeception

Linux:

wget http://codeception.com/codecept.phar .

php codecept.phar bootstrap

Windows:

php composer.phar global require “codeception/codeception:*”

Composer/ PhpStorm:

php composer.phar require “codeception/codeception:*”

Getting readyBootStrap

>codecept bootstrap

>codecept generate:cept acceptance Validation

Edit the configuration file : tests/acceptance.suite.yml

class_name: AcceptanceTester

modules:

enabled:

- PhpBrowser

- AcceptanceHelper

config:

PhpBrowser:

url: ‘http://conference.kphpug.jp/2015/’

Let’s get the party started>codecept run

>codecept run —debug

Bonus

>codecept generate:scenarios acceptance //English version

>codecept console acceptance //Interactive console

>codecept run —debug —env test

Demo Time !!!!!> Let’s test PHP conference Kansai website !

Best practices1. Run tests on your vagrant -> GIT push ONLY when tests pass

2. Deploy on test server -> Validate tests on test server

3. Deploy on production ONLY if tests pass on test server

4. NEVER run on production ! NEVER!!!

1. It creates new entries and change Database

2. Add load on the production server

3. Need new set of tests/monitoring on production

ReferencesInstallation : http://codeception.com/install

Acceptance Tests : http://codeception.com/docs/04-AcceptanceTests

Yii2 module : http://codeception.com/docs/modules/Yii2

WebDriver : http://codeception.com/docs/modules/WebDriver

Yii2 Database Faker : http://qiita.com/tanakahisateru/items/c4d39cc77a71067ed658

Other toolsPHPUnit Framework(Difficult) phpspec (Specification & Unit Testing) Pure Selenium/PhantomJS (Harder integration) Mink (Browser Controlling, slow,harder integration)

Questions & AnswersDo you have any questions?