Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
1
BDD with Behat &
Drupal
Alessio Piazza
Software Developer @SparkFabrik
(twinbit + agavee)
twitter: @alessiopiazza
I do stu...
3
• Behaviour Driven Development
• a software development process that emerged
from TDD (Test Driven Development)
• Behavi...
WHY BDD? or TDD?
• You must test your application
• Save your self from regressions (fix one thing,
break another one)
• Sa...
BDD Workflow
1. Write a specification feature
2. Implement its test
3. Run test and watch it FAIL
4. Change your app to make...
BDD: HOW IT WORKS?
6
HOW IT WORKS?
7
Write a specification feature
8
Feature: Download Drupal 8
In order to use Drupal 8
as a user
i should be able to download ...
SCENARIO STRUCTURE
9
Scenario: Download Drupal 8 from Drupal.org
Given I am on drupal.org homepage
When I Click “Download ...
SCENARIO STRUCTURE
10
Given i am on drupal.org homepage
And i Click “Download and Extend”
Then I should see the link “Down...
SCENARIO STRUCTURE
11
Given i am on drupal.org homepage
And i Click “Download and Extend”
Then I should see the link “Down...
STEPS: Keywords
12
Given -> To put the system in a known state
Given I am an anonymous user
SPARKFABRIK
Given
STEPS: Keywords
13
When -> To describe the key action to performs
When I click on the link
SPARKFABRIK
Given
STEPS: Keywords
14
When
Then -> To to observe outcomes
Then I should see the text “Hello”
SPARKFABRIK
Given
STEPS: Keywords
15
When
Then
And / But -> To make our scenario more readable
SPARKFABRIK
STEPS: Keywords
16
Scenario: Download Drupal 8 from Drupal.org
Given I am on drupal.org homepage
Given I am an anonymous u...
STEPS: Keywords
17
Scenario: Download Drupal 8 from Drupal.org
Given I am on drupal.org homepage
And I am an anonymous use...
GHERKIN
• It is a Business Readable, Domain Specific
Language
• Lets you describe software’s behaviour without
detailing ho...
BEHAT
docs.behat.org/en/v3.0/
Open source Behavior Driven Development
framework for PHP (5.3+)
Inspired from Cucumber (Rub...
HOW BEHAT READS
GHERKIN?
20
Given I am an anonymous user
SPARKFABRIK
HOW BEHAT READS
GHERKIN?
21
/**
* @Given I am an anonymous user
*/
public function assertAnonymousUser() {
// Verify the u...
HOW BEHAT READS
GHERKIN?
22
Then I should not see the text :text
SPARKFABRIK
HOW BEHAT READS
GHERKIN?
23
/**
* @Then I should not see the text :text
*/
public function assertNotTextVisible($text) {
/...
STEP RESULTS
24
/**
* @Then I should not see the text :text
*/
public function assertNotTextVisible($text) {
// This step ...
STEP RESULTS
25
/**
* @Then I will trigger a success
*/
public function triggerSuccess() {
// This step will pass.
print(‘...
OTHER STEP RESULTS
26
• SUCCESSFUL
• FAILED
• PENDING
• UNDEFINED
• SKIPPED
• AMBIGUOUS
• REDUNDANT
SPARKFABRIK
WHERE ARE THESE
STEPS?
27
• STEPS are defined inside plain PHP Object
classes, called CONTEXTS
• MinkContext and DrupalCont...
Sum Up
28
Give use the syntax
to write features and scenarios
Our framework that reads our
scenarios and run tests
Classes...
How we integrate Behat and
Drupal?
29
Behat Drupal Extension
An integration layer between
Drupal and Behat
https://www.dru...
From your project root..
composer require drupal/drupal-extension
30
- Installing drupal/drupal-driver (v1.1.4)
Downloadin...
Create file behat.yml
31
1 default:
2 suites:
3 default:
4 contexts:
5 - FeatureContext
6 - DrupalDrupalExtensionContextDru...
Then --init your test suite
32
vendor/bin/behat --init
+d features - place your *.feature files here
+d features/bootstrap...
Demo!
33
SPARKFABRIK
BROWSER INTERACTION
• With BDD we write clear, understandable
scenario
• Our scenario often describe an interaction with
t...
BROWSER EMULATORS
• Headless browser emulators (Goutte)
• Browser controllers (Selenium2, SAHI)
• They do basically the sa...
MINK!
http://mink.behat.org/en/latest/
• MINK removes API differences between different browser emulators
• MINK provides ...
37
/**
* @When /^(?:|I )move forward one page$/
*/
public function forward() {
$this->getSession()->forward();
}
MINK!
htt...
Wrap up
38
GHERKIN
BEHAT
CONTEXTS
MINK
Give use the syntax
to write features and scenarios
Our framework that reads our
sc...
39
Upcoming SlideShare
Loading in …5
×

Behaviour Driven Development con Behat & Drupal

414 views

Published on

Il Behaviour Driven Development è una pratica di sviluppo software nella quale i comportamenti della propria applicazione vengono descritti con un linguaggio chiaro e comprensibile a tutti. Grazie a Behat, questi scenari si trasformano in test veri e propri che possono essere eseguiti sulle nostre applicazioni Drupal.

Published in: Software
  • Be the first to comment

Behaviour Driven Development con Behat & Drupal

  1. 1. 1
  2. 2. BDD with Behat & Drupal Alessio Piazza Software Developer @SparkFabrik (twinbit + agavee) twitter: @alessiopiazza I do stuff with Drupal SPARKFABRIK
  3. 3. 3 • Behaviour Driven Development • a software development process that emerged from TDD (Test Driven Development) • Behaviour driven development specifies that tests of any unit of software should be specified in terms of the desired behaviour of the unit BDD SPARKFABRIK
  4. 4. WHY BDD? or TDD? • You must test your application • Save your self from regressions (fix one thing, break another one) • Save your self from misunderstanding between you and your client • Your tests become specification 4 SPARKFABRIK
  5. 5. BDD Workflow 1. Write a specification feature 2. Implement its test 3. Run test and watch it FAIL 4. Change your app to make test PASS 5. Run test and watch it PASS 6. Repat from 2-5 until all GREEN 5 SPARKFABRIK
  6. 6. BDD: HOW IT WORKS? 6
  7. 7. HOW IT WORKS? 7
  8. 8. Write a specification feature 8 Feature: Download Drupal 8 In order to use Drupal 8 as a user i should be able to download it from drupal.org Scenario: Download Drupal 8 from Drupal.org Given i am on the homepage When i Click “Download and Extend” Then I should see the link “Download Drupal 8.0.0” Scenario: … SPARKFABRIK
  9. 9. SCENARIO STRUCTURE 9 Scenario: Download Drupal 8 from Drupal.org Given I am on drupal.org homepage When I Click “Download and Extend” Then I should see the link “Download Drupal 8.0.0” SPARKFABRIK
  10. 10. SCENARIO STRUCTURE 10 Given i am on drupal.org homepage And i Click “Download and Extend” Then I should see the link “Download Drupal 8.0.0” Scenario: Download Drupal 8 from Drupal.org Keyword Description SPARKFABRIK
  11. 11. SCENARIO STRUCTURE 11 Given i am on drupal.org homepage And i Click “Download and Extend” Then I should see the link “Download Drupal 8.0.0” Scenario: Download Drupal 8 from Drupal.org Keyword Description Steps Given I am on drupal.org homepage When I Click “Download and Extend” Then I should see the link “Download …” SPARKFABRIK
  12. 12. STEPS: Keywords 12 Given -> To put the system in a known state Given I am an anonymous user SPARKFABRIK
  13. 13. Given STEPS: Keywords 13 When -> To describe the key action to performs When I click on the link SPARKFABRIK
  14. 14. Given STEPS: Keywords 14 When Then -> To to observe outcomes Then I should see the text “Hello” SPARKFABRIK
  15. 15. Given STEPS: Keywords 15 When Then And / But -> To make our scenario more readable SPARKFABRIK
  16. 16. STEPS: Keywords 16 Scenario: Download Drupal 8 from Drupal.org Given I am on drupal.org homepage Given I am an anonymous user When I Click “Download and Extend” When I Click “Download Drupal 8.0.0” Then I should see the link “Drupal Core 8.0.0” SPARKFABRIK
  17. 17. STEPS: Keywords 17 Scenario: Download Drupal 8 from Drupal.org Given I am on drupal.org homepage And I am an anonymous user When I Click “Download and Extend” And I Click “Download Drupal 8.0.0” Then I should see the link “Drupal Core 8.0.0” SPARKFABRIK
  18. 18. GHERKIN • It is a Business Readable, Domain Specific Language • Lets you describe software’s behaviour without detailing how that behaviour is implemented. • Gherkin is the language that BEHAT understands. 18 SPARKFABRIK
  19. 19. BEHAT docs.behat.org/en/v3.0/ Open source Behavior Driven Development framework for PHP (5.3+) Inspired from Cucumber (Ruby) www.cucumber.io From version 3 BEHAT it’s an official Cucumber implementation in PHP Let us write tests based on our scenarios 19 SPARKFABRIK
  20. 20. HOW BEHAT READS GHERKIN? 20 Given I am an anonymous user SPARKFABRIK
  21. 21. HOW BEHAT READS GHERKIN? 21 /** * @Given I am an anonymous user */ public function assertAnonymousUser() { // Verify the user is logged out. if ($this->loggedIn()) { $this->logout(); } } Given I am an anonymous user SPARKFABRIK
  22. 22. HOW BEHAT READS GHERKIN? 22 Then I should not see the text :text SPARKFABRIK
  23. 23. HOW BEHAT READS GHERKIN? 23 /** * @Then I should not see the text :text */ public function assertNotTextVisible($text) { // Use the Mink Extension step definition. $this->assertPageNotContainsText($text); } Then I should not see the text :text SPARKFABRIK
  24. 24. STEP RESULTS 24 /** * @Then I should not see the text :text */ public function assertNotTextVisible($text) { // This step will fail. throw new Exception(‘Test failed’); } • A step FAILS when an Exception is thrown SPARKFABRIK
  25. 25. STEP RESULTS 25 /** * @Then I will trigger a success */ public function triggerSuccess() { // This step will pass. print(‘Hello DrupalDay’); } • A step PASS when no Exception are raised SPARKFABRIK
  26. 26. OTHER STEP RESULTS 26 • SUCCESSFUL • FAILED • PENDING • UNDEFINED • SKIPPED • AMBIGUOUS • REDUNDANT SPARKFABRIK
  27. 27. WHERE ARE THESE STEPS? 27 • STEPS are defined inside plain PHP Object classes, called CONTEXTS • MinkContext and DrupalContext give use a lot of pre-defined steps ready to be used • Custom steps can be defined inside FeatureContext class SPARKFABRIK
  28. 28. Sum Up 28 Give use the syntax to write features and scenarios Our framework that reads our scenarios and run tests Classes containing our step definitions GHERKIN BEHAT CONTEXTS SPARKFABRIK
  29. 29. How we integrate Behat and Drupal? 29 Behat Drupal Extension An integration layer between Drupal and Behat https://www.drupal.org/project/drupalextension https://github.com/jhedstrom/drupalextension SPARKFABRIK
  30. 30. From your project root.. composer require drupal/drupal-extension 30 - Installing drupal/drupal-driver (v1.1.4) Downloading: 100% > DrupalCoreComposerComposer::vendorTestCodeCleanup - Installing symfony/filesystem (v3.0.0) Downloading: 100% > DrupalCoreComposerComposer::vendorTestCodeCleanup - Installing symfony/config (v2.8.0) Downloading: 100% > DrupalCoreComposerComposer::vendorTestCodeCleanup - Installing behat/transliterator (v1.1.0) Loading from cache … … > DrupalCoreComposerComposer::vendorTestCodeCleanup - Installing drupal/drupal-extension (v3.1.3) Downloading: 100% SPARKFABRIK
  31. 31. Create file behat.yml 31 1 default: 2 suites: 3 default: 4 contexts: 5 - FeatureContext 6 - DrupalDrupalExtensionContextDrupalContext 7 - DrupalDrupalExtensionContextMinkContext 8 extensions: 9 BehatMinkExtension: 10 goutte: ~ 11 base_url: http://d8.drupalday2015.sparkfabrik.loc # Replace with your site's URL 12 DrupalDrupalExtension: 13 blackbox: ~ see https://github.com/jhedstrom/drupalextension SPARKFABRIK
  32. 32. Then --init your test suite 32 vendor/bin/behat --init +d features - place your *.feature files here +d features/bootstrap - place your context classes here +f features/bootstrap/FeatureContext.php - place your definitions, transformations and hooks here SPARKFABRIK
  33. 33. Demo! 33 SPARKFABRIK
  34. 34. BROWSER INTERACTION • With BDD we write clear, understandable scenario • Our scenario often describe an interaction with the browser 34 WE NEED A BROWSER EMULATOR SPARKFABRIK
  35. 35. BROWSER EMULATORS • Headless browser emulators (Goutte) • Browser controllers (Selenium2, SAHI) • They do basically the same thing: control or emulate browsers but they do them in different ways • They comes with their pro and cons 35 SPARKFABRIK
  36. 36. MINK! http://mink.behat.org/en/latest/ • MINK removes API differences between different browser emulators • MINK provides different drivers for every browser emulator • MINK gives you an easy way to 1. control the browser 2. traverse pages 3. manipulate page elements 4. interact with page elements 36 SPARKFABRIK
  37. 37. 37 /** * @When /^(?:|I )move forward one page$/ */ public function forward() { $this->getSession()->forward(); } MINK! http://mink.behat.org/en/latest/ When I move forward one page SPARKFABRIK
  38. 38. Wrap up 38 GHERKIN BEHAT CONTEXTS MINK Give use the syntax to write features and scenarios Our framework that reads our scenarios and run the tests Classes containing our step definitions Describe browser interaction without worrying about the browser SPARKFABRIK
  39. 39. 39

×