Upgrade to Pro — share decks privately, control downloads, hide ads and more …

BDD - Behavior Driven Development

BDD - Behavior Driven Development

What is BDD and how to implement it with Behat.

Reputation VIP

April 15, 2015
Tweet

More Decks by Reputation VIP

Other Decks in Programming

Transcript

  1. http://reputationvip.io/ Introduction  Invent in 2003 by Dan North as

    the TDD (Test Driven Development) continuity  Emphasize on behavior  Make the link between functionalities and tests 3
  2. http://reputationvip.io/ A UNIQUE VOCABULARY Story : User Story definition Scenario

    1 : I want to test such case Given : Given these values : […] When : When I do this action Then : Then I've these results : […] 4
  3. http://reputationvip.io/ Example 5 Feature: Loan books As a library subscriber,

    I want to loan books Scenario: Loan 1 book Given books : | name | | Da Vinci Code | | The Little Prince | | Harry Potter 1 | Given users : | name | | John | | Guillermo | When the user "John" loan the book "The Little Prince" Then "2" books are availables in the library Then user "John" have "1" book loaned
  4. http://reputationvip.io/ Why using Behat ?  Describe behavior by a

    language readable by everyone.  Increase understanding between developers and PO (Product Owner)  Every User Story are describes, in the form of automatics tests.  Developed code answer perfectly to the User Story  No regressions  Help the PO to describe User Stories well  Increase User stories accuracy  Can be use as documentation and examples 6
  5. http://reputationvip.io/ How to use BDD ?  Answer 1 :

     PO write BDD test  Answer 2 :  Developers write BDD tests from User Stories  Answer 3 (hybrid) :  Developers write BDD tests from User Stories and PO valid scenarios 7
  6. http://reputationvip.io/ Tools  PHP : Behat3 (>=PHP 5.3)  Java

    : Jbehave, EasyB, Concordion …  .NET : Nspec, xBehav, Nbehave …  More than 30 framework ! 9
  7. http://reputationvip.io/ Unit Test / BDD 10 Unit Test BDD Architecture

    Behavior What is testing ? Inside the system Outside of the system How to test ? Require to modify tests Nothing to change If architecture change ? Can't access Easy to read, write and understand For the PO ?
  8. http://reputationvip.io/ Behat installation  Composer  Initialisation => Folder «

    features/ » is created |- - features - - bootstrap - - FeatureContext.php 11 "require-dev": { "behat/behat": "3.*" }, "config": { "bin-dir": "bin/" }
  9. http://reputationvip.io/ How to write Fixtures ? (1/2)  gherkin language

     Files <test_name>.feature on folder « features »  Key words : 12 Feature: Loan books As a library subscriber, I want to loan books Scenario: Can't loan more than 2 books […] Scenario: Loan 1 book […]
  10. http://reputationvip.io/ How to write Fixtures ? (2/2) 13 # language:

    fr Given the book "Da Vinci Code" Background : Given the book "Da Vinci Code" When the user "John" loan the book "Da Vinci Code" Then "2" books are availables in the library Then user "John" have "1" book loaned @dev Scenario: Can't loan more than 2 books
  11. http://reputationvip.io/ Write Steps (1/2)  <class_name>Context.php (default : FeatureContext.php) 

    Auto-generated by PHPStorm  Example : 14 Given the book "Da Vinci Code" /** * @Given the book :arg1 */ public function theBook($arg1) { }
  12. http://reputationvip.io/ Write Steps (2/2) 15 /** * @Given users :

    */ public function users(TableNode $users) { foreach ($users as $user) { $name = $user['name']; } } Given users : | name | | John | | Guillermo |
  13. http://reputationvip.io/ Hooks 16 /** @BeforeSuite */ public static function setup(BeforeSuiteScope

    $scope) /** @AfterSuite */ public static function teardown(AfterSuiteScope $scope) /** @BeforeFeature */ public static function setupFeature(BeforeFeatureScope $scope) /** @AfterFeature */ public static function teardownFeature(AfterFeatureScope $scope) /** @BeforeScenario */ public function before(BeforeScenarioScope $scope) /** @AfterScenario */ public function after(AfterScenarioScope $scope)
  14. http://reputationvip.io/ Run tests  All test :  1 feature

    :  1 scenario :  Run tests with a special tag : 17
  15. http://reputationvip.io/ Behat and Symfony (1/2) 18  composer.json : 

    behat.yml :  Initialisation :  Folder « features» created in root project  Folder « Features » created in AppBundle "require-dev": { "behat/behat": "3.*", "behat/symfony2-extension": "*@stable" } default: extensions: Behat\Symfony2Extension: ~ suites: test_suite: type: symfony_bundle bundle: 'AppBundle' contexts : - AppBundle\Features\Context\FeatureContext - AppBundle\Features\Context\LoanContext: kernel: '@kernel' em: '@doctrine.orm.default_entity_manager' LoanService: '@loan_service'
  16. http://reputationvip.io/ Behat and Symfony (2/2) 19 class LoanContext extends \PHPUnit_Framework_TestCase

    implements Context { public function __construct(AppKernel $kernel, EntityManager $em, LoanService $LoanService) { }
  17. http://reputationvip.io/ Behat and PHPStorm 20  Settings : PHP/Behat :

     Define the parameter of path to bin/behat  Define the parameter of path to behat.yml  Test result :
  18. http://reputationvip.io/ Code coverage  Doesn’t exist by default in Behat.

     Code coverage is it really usefull on BDD ? 21 trait CodeCoverage { /** @BeforeSuite */ public static function suiteSetup($event) { xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE); } /** @AfterSuite */ public static function suiteTeardown($event) { $data = xdebug_get_code_coverage(); xdebug_stop_code_coverage(); $filter = new PHP_CodeCoverage_Filter(); $kernel = new AppKernel("test", true); $filter->addDirectoryToWhitelist($kernel->getRootDir() . "/../src"); $coverage = new PHP_CodeCoverage(null, $filter); $coverage->append($data, time()); $writer = new PHP_CodeCoverage_Report_HTML; $writer->process($coverage, './code-coverage-report'); $writer = new PHP_CodeCoverage_Report_Clover; $writer->process($coverage, './code-coverage-report.xml'); $writer = new PHP_CodeCoverage_Report_Text(75, 90, false, false); fwrite(STDOUT, $writer->process($coverage, true)); } }
  19. http://reputationvip.io/ Behat and Mink  User interface test  Allow

    to simulate a browser or control a real browser 22
  20. http://reputationvip.io/ Conclusion 24  It's logically the next step after

    TDD  Highlight the behavior  Make the specifications understanding easier  Test full scenario instead of small units  Takes more short-term time but increase productivity after  Need a test environment