Text
Introducing Codeception
(TDD)—>Test Driven Development
Agenda
Presentation 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 types
Acceptance
Tests
Functional
Tests
Unit Tests
Yii2 BackendFrontend
Browser
Selenium
PhantomJS
HTML
HTML
$_GET,$_POST,
$_REQUEST
Yii2 Module Backend
Different tests types
Acceptance
Tests
Functional
Tests
Unit Tests
Yii2 BackendFrontend
Browser
Selenium
PhantomJS
HTML
HTML
$_GET,$_POST,
$_REQUEST
Yii2 Module Backend
Different tests types
Acceptance
Tests
Functional
Tests
Unit Tests
Yii2 BackendFrontend
Browser
Selenium
PhantomJS
HTML
HTML
$_GET,$_POST,
$_REQUEST
Yii2 Module Backend
User Scenario
Different tests types
Acceptance
Tests
Functional
Tests
Unit Tests
Yii2 BackendFrontend
Browser
Selenium
PhantomJS
HTML
HTML
$_GET,$_POST,
$_REQUEST
Yii2 Module Backend
User Scenario
Different tests types
Acceptance
Tests
Functional
Tests
Unit Tests
Yii2 BackendFrontend
Browser
Selenium
PhantomJS
HTML
HTML
$_GET,$_POST,
$_REQUEST
Yii2 Module Backend
User Scenario
Developer Scenario
Different tests types
Acceptance
Tests
Functional
Tests
Unit Tests
Yii2 BackendFrontend
Browser
Selenium
PhantomJS
HTML
HTML
$_GET,$_POST,
$_REQUEST
Yii2 Module Backend
User Scenario
Developer Scenario
Different tests types
Acceptance
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 Tests
Same 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 Tests
Used 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 platforms
Mac 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 ready
BootStrap
>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 practices
1. 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
References
Installation : 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 tools
PHPUnit Framework(Difficult)
phpspec (Specification & Unit Testing)
Pure Selenium/PhantomJS (Harder integration)
Mink (Browser Controlling, slow,harder integration)
Questions & Answers
Do you have any questions?

Codeception Testing Framework -- English #phpkansai

  • 1.
  • 2.
    Agenda Presentation of Speaker/Company Presentation of Codeception Presentation of Tests Types Demonstration & Hands-on Best Practices References Q & A @phpcon_kansai
  • 3.
    Who the hellam 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….
  • 4.
    Who the hellam 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….
  • 5.
    Who the hellam 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….
  • 6.
    Who the hellam 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….
  • 7.
    Who the hellam 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….
  • 8.
    Who the hellam 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….
  • 9.
    Who the hellam 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….
  • 10.
    Why do ispeak 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
  • 11.
    Why do ispeak 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
  • 12.
    Why do ispeak 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
  • 13.
    Why do ispeak 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
  • 14.
    Who do Iwork for? Cook+biz Awesome Team Awesome Challenges
  • 15.
    Who do Iwork for? Cook+biz Awesome Team Awesome Challenges
  • 16.
    Who do Iwork for? Cook+biz Awesome Team Awesome Challenges
  • 17.
    Why Testing? Why shouldI 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
  • 18.
    CodeCeption(1) http://codeception.com/ (English) Test Framework AcceptanceTests Functional Tests Unit Tests Easy To read To write To debug Natively handles PHPUnit -> standard
  • 19.
    CodeCeption(2) BDD-style scenario-driver tests WebServicestests (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
  • 20.
    What does itlook 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')); ?>
  • 22.
    Different tests types Acceptance Tests Functional Tests UnitTests Yii2 BackendFrontend Browser Selenium PhantomJS HTML HTML $_GET,$_POST, $_REQUEST Yii2 Module Backend
  • 23.
    Different tests types Acceptance Tests Functional Tests UnitTests Yii2 BackendFrontend Browser Selenium PhantomJS HTML HTML $_GET,$_POST, $_REQUEST Yii2 Module Backend
  • 24.
    Different tests types Acceptance Tests Functional Tests UnitTests Yii2 BackendFrontend Browser Selenium PhantomJS HTML HTML $_GET,$_POST, $_REQUEST Yii2 Module Backend User Scenario
  • 25.
    Different tests types Acceptance Tests Functional Tests UnitTests Yii2 BackendFrontend Browser Selenium PhantomJS HTML HTML $_GET,$_POST, $_REQUEST Yii2 Module Backend User Scenario
  • 26.
    Different tests types Acceptance Tests Functional Tests UnitTests Yii2 BackendFrontend Browser Selenium PhantomJS HTML HTML $_GET,$_POST, $_REQUEST Yii2 Module Backend User Scenario Developer Scenario
  • 27.
    Different tests types Acceptance Tests Functional Tests UnitTests Yii2 BackendFrontend Browser Selenium PhantomJS HTML HTML $_GET,$_POST, $_REQUEST Yii2 Module Backend User Scenario Developer Scenario
  • 28.
    Different tests types Acceptance Tests Functional Tests UnitTests Yii2 BackendFrontend Browser Selenium PhantomJS HTML HTML $_GET,$_POST, $_REQUEST Yii2 Module Backend User Scenario Developer Scenario Function Testing
  • 29.
    Acceptance Tests Enable todo 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
  • 30.
    Functional Tests Same asAcceptance 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)
  • 31.
    Unit Tests Used totest 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'])); }
  • 32.
    Install on allplatforms Mac 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:*”
  • 33.
    Getting ready BootStrap >codecept bootstrap >codeceptgenerate: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/’
  • 34.
    Let’s get theparty started >codecept run >codecept run —debug Bonus >codecept generate:scenarios acceptance //English version >codecept console acceptance //Interactive console >codecept run —debug —env test
  • 35.
    Demo Time !!!!! >Let’s test PHP conference Kansai website !
  • 36.
    Best practices 1. Runtests 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
  • 37.
    References Installation : http://codeception.com/install AcceptanceTests : 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 tools PHPUnit Framework(Difficult) phpspec (Specification & Unit Testing) Pure Selenium/PhantomJS (Harder integration) Mink (Browser Controlling, slow,harder integration)
  • 38.
    Questions & Answers Doyou have any questions?