BDD Tools – What is BDD? – tests written in a natural language



BDD Tools – What is BDD? – tests written in a natural language

0 0


bdd-tools-presentation


On Github zroger / bdd-tools-presentation

BDD Tools

What is BDD?

“Behaviour-driven development is an “outside-in” methodology. It starts at the outside by identifying business outcomes, and then drills down into the feature set that will achieve those outcomes. Each feature is captured as a “story”, which defines the scope of the feature along with its acceptance criteria.”

Dan North, "What's in a Story"

What is BDD?

“Behavior-driven development (or BDD) is an agile software development technique that encourages collaboration between developers, QA and non-technical or business participants in a software project.”

http://pythonhosted.org/behave/

What is BDD?

“BDD provides a “ubiquitous language” for analysis” “Acceptance criteria should be executable”

Dan North, "Introducing BDD"

What is BDD?

“behave uses tests written in a natural language style, backed up by Python code.”

http://pythonhosted.org/behave/

tests written in a natural language

backed up by code

Gherkin

It is a Business Readable, Domain Specific Language that lets you describe software’s behaviour without detailing how that behaviour is implemented.

https://github.com/cucumber/cucumber/wiki/Gherkin

Given/When/Then

  • Given: put the system in a known state.

  • When: describe the key action.

  • Then: observe outcomes.

Feature: Some terse yet descriptive text of what is desired
  Textual description of the business value of this feature
  Business rules that govern the scope of the feature
  Any additional information that will make the feature easier to understand

  Scenario: Some determinable business situation
    Given some precondition
      And some other precondition
     When some action by the actor
      And some other action
      And yet another action
     Then some testable outcome is achieved
      And something else we can check happens too

  Scenario: A different situation
      ...
Feature: Division
  In order to avoid silly mistakes
  Cashiers must be able to calculate a fraction

  Scenario: Regular numbers
    Given I have entered 3 into the calculator
    And I have entered 2 into the calculator
    When I press divide
    Then the result should be 1.5 on the screen
Feature: Addition
  In order to avoid silly mistakes
  As a math idiot 
  I want to be told the sum of two numbers

  Scenario Outline: Add two numbers
    Given I have entered <input_1> into the calculator
    And I have entered <input_2> into the calculator
    When I press <button>
    Then the result should be <output> on the screen

  Examples:
    | input_1 | input_2 | button | output |
    | 20      | 30      | add    | 50     |
    | 2       | 5       | add    | 7      |
    | 0       | 40      | add    | 40     |

tests written in a natural language

backed up by code

Implementation (Ruby cucumber)

Scenario: Blenders
  Given I put "apples" in a blender
  When  I switch the blender on
  Then  it should transform into "apple juice"
Given /I put "([^"]*)" in a blender/ do |thing|
    blender = Blender.new
    blender.add(thing)
end

Implementation (PHP behat)

Scenario: Blenders
  Given I put "apples" in a blender
  When  I switch the blender on
  Then  it should transform into "apple juice"
/**
 * @Given /^I put "([^"]*)" in a blender$/
 */
public function iPutSomethingInABlender($thing) {
    $this->blender = new Blender()
    $blender->add($thing)
}

Implementation (python behave)

Scenario: Blenders
  Given I put "apples" in a blender
  When  I switch the blender on
  Then  it should transform into "apple juice"
@given('I put "{thing}" in a blender')
def step_given_put_thing_into_blender(context, thing):
    context.blender = Blender()
    context.blender.add(thing)

@when('I switch the blender on')
def step_when_switch_blender_on(context):
    context.blender.switch_on()

@then('it should transform into "{other_thing}"')
def step_then_should_transform_into(context, other_thing):
    assert_that(context.blender.result, equal_to(other_thing))

Testing Domains

  • Web
    • Capybara, Watir (ruby)
    • Mink (PHP)
    • Behaving (Python)
  • CLI
    • Aruba (ruby)
  • Email, SMS
    • Behaving (python)

Browser steps (behaving)

Feature: Google Search

Background:
  Given a browser

Scenario: Search for Warby Parker
  Given I go to "http://google.com"
  When I fill in "q" with "warby parker"
  And I press "I'm Feeling Lucky"
  Then the browser's url should be "http://www.warbyparker.com"

Browser steps (behaving)

Given a browser
Given brand as the default browser
Given browser "name"
When I reload
When I go back
When I go forward
When I visit "url"
When I go to "url"
Then the browsers url should be "url"
When I click the link with text that contains "text"
Then I should see an element with the css selector "selector"

CLI testing with Aruba

Feature: exit statuses

  Scenario: exit status of 0
    When I run "ruby -h"
    Then the exit status should be 0

  Scenario: non-zero exit status
    When I run "ruby -e 'exit 56'"
    Then the exit status should be 56

CLI testing with Aruba

Given I am using a clean gemset "gemset"
Given a directory named "dir"
Given a file named "file" with: 
Given an empty file named "file"
When I cd to "dir"
When I run `command`
When I successfully run `command`
When I type "string"
Then the output should contain "text"
Then the output should not contain "text"
Then the exit status should be code
Then the exit status should not be code

Examples

Drupal/Behat

Feature: Testing demo
  Scenario: Visit the user login page
    Given I am not logged in
    When I visit "/user"
    Then I should see the link "request a new password"

  @api
  Scenario: As a logged in user
    Given I am logged in as a user with the "administrator" role
    When I visit "/admin"
    Then I should see the link "Content"

THE END