Top Banner
Text Introducing Codeception (TDD)—>Test Driven Development
38
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Codeception Testing Framework -- English #phpkansai

Text

Introducing Codeception (TDD)—>Test Driven Development

Page 2: Codeception Testing Framework -- English #phpkansai

AgendaPresentation of Speaker/ Company

Presentation of Codeception

Presentation of Tests Types

Demonstration & Hands-on

Best Practices

References

Q & A

@phpcon_kansai

Page 3: Codeception Testing Framework -- English #phpkansai

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

Page 4: Codeception Testing Framework -- English #phpkansai

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

Page 5: Codeception Testing Framework -- English #phpkansai

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

Page 6: Codeception Testing Framework -- English #phpkansai

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

Page 7: Codeception Testing Framework -- English #phpkansai

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

Page 8: Codeception Testing Framework -- English #phpkansai

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

Page 9: Codeception Testing Framework -- English #phpkansai

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

Page 10: Codeception Testing Framework -- English #phpkansai

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

Page 11: Codeception Testing Framework -- English #phpkansai

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

Page 12: Codeception Testing Framework -- English #phpkansai

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

Page 13: Codeception Testing Framework -- English #phpkansai

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

Page 14: Codeception Testing Framework -- English #phpkansai

Who do I work for?Cook+biz

Awesome Team

Awesome Challenges

Page 15: Codeception Testing Framework -- English #phpkansai

Who do I work for?Cook+biz

Awesome Team

Awesome Challenges

Page 16: Codeception Testing Framework -- English #phpkansai

Who do I work for?Cook+biz

Awesome Team

Awesome Challenges

Page 17: Codeception Testing Framework -- English #phpkansai

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

Page 18: Codeception Testing Framework -- English #phpkansai

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

Page 19: Codeception Testing Framework -- English #phpkansai

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

Page 20: Codeception Testing Framework -- English #phpkansai

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')); ?>

Page 21: Codeception Testing Framework -- English #phpkansai
Page 22: Codeception Testing Framework -- English #phpkansai

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

Page 23: Codeception Testing Framework -- English #phpkansai

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

Page 24: Codeception Testing Framework -- English #phpkansai

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

User Scenario

Page 25: Codeception Testing Framework -- English #phpkansai

Different tests typesAcceptance

Tests

Functional Tests

Unit Tests

Yii2 BackendFrontend

Browser Selenium

PhantomJS

HTML

HTML

$_GET,$_POST,

$_REQUEST

Yii2 Module Backend

User Scenario

Page 26: Codeception Testing Framework -- English #phpkansai

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

Page 27: Codeception Testing Framework -- English #phpkansai

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

Page 28: Codeception Testing Framework -- English #phpkansai

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

Page 29: Codeception Testing Framework -- English #phpkansai

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

Page 30: Codeception Testing Framework -- English #phpkansai

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)

Page 31: Codeception Testing Framework -- English #phpkansai

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'])); }

Page 32: Codeception Testing Framework -- English #phpkansai

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:*”

Page 33: Codeception Testing Framework -- English #phpkansai

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/’

Page 34: Codeception Testing Framework -- English #phpkansai

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

Page 35: Codeception Testing Framework -- English #phpkansai

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

Page 36: Codeception Testing Framework -- English #phpkansai

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

Page 37: Codeception Testing Framework -- English #phpkansai

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)

Page 38: Codeception Testing Framework -- English #phpkansai

Questions & AnswersDo you have any questions?