Cucumber & BDD

34
Cucumber Sam Davarnia @samdvr samdvr.com

description

Cucumber and TDD talk at SoCal Codecamp 2014 - USC

Transcript of Cucumber & BDD

Page 1: Cucumber & BDD

CucumberSam Davarnia

@samdvr samdvr.com

Page 2: Cucumber & BDD

About

Sr.Web Developer working mostly with Ruby on Rails.

Interests: Databases, Object Oriented Design, Testing, Concurrency and Scalability.

@samdvr

samdvr.com

Page 3: Cucumber & BDD

I. Test  Driven  Development  II.Behavior  Driven  Development  III.Cucumber

Page 4: Cucumber & BDD

Why write tests?More Confidence

Feedback

Reduce Bugs

Better Software Design

Refactoring

Page 5: Cucumber & BDD

Unit Test

Unit  Under  Test

Mock

Mock

Page 6: Cucumber & BDD

Integration Test

Page 7: Cucumber & BDD

Unit  Tests

Integration

Page 8: Cucumber & BDD

Test Driven Development

Kent Beck

Test First Development

Page 9: Cucumber & BDD

TDD Cycle

WRITE A FAILING TEST

MAKE THE TEST PASS

REFACTOR

Page 10: Cucumber & BDD

Red

Write a failing test

Reasoning

Verify that our test is validating what we want

Page 11: Cucumber & BDD

Green

Write the simplest implementation that makes the test pass

Page 12: Cucumber & BDD

Refactor

Improve the internal structure of code.

Page 13: Cucumber & BDD

Myths about TDD

TDD is time consuming

Code is hard to test

Tests are slow

Page 14: Cucumber & BDD

Myths about TDD

TDD is

time consuming

Page 15: Cucumber & BDD

Myths about TDDCode is hard to test

Bad/No software Design

Too many dependencies

Queries combined with Commands

Violating The Law of Demeter

and …

Page 16: Cucumber & BDD

Myths about TDD

Tests are slow

Lack of isolation

Test Brittleness

Page 17: Cucumber & BDD

Behavior Driven Development

• Focus on specifications • Unified vocabulary • Outside In • User Centric • Good TDD practitioners are BDD

practitioners

Page 18: Cucumber & BDD

What’s CucumberBDD Framework for integration tests

Easy to read syntax

Bridge the gap between stakeholder and developer

It can use a web driver or be isolated

It can test ruby, .Net, PHP and Java

Page 19: Cucumber & BDD

Starting CucumberAfter  Installing  the  cucumber  gem,  We  make  a  directory  called  features  Each  scenario  is  in  this  directory  with  .feature  extension.  

..  features/books.feature  ..

Page 20: Cucumber & BDD

Gherkin Syntax

Feature:  <General  purpose  of  a  feature>      Scenario:  <Scenario  in  the  feature>          Given  <Context>          When  <Action>  

Then  <Expectation>          

Page 21: Cucumber & BDD

Gherkin Syntax

Feature:  Manage  Books      Scenario:  Admin  User  can  create  books          Given  I  am  an  admin  user          When  I  create  a  book  ‘1984’  

And    visit  the  homepage  Then  I  should  see  ‘1984’    

     

Page 22: Cucumber & BDD

Running CucumberYou  can  implement  step  definitions  for  undefined  steps  with  these  snippets:  

Given(/^I  am  an  admin  user$/)  do      pending  #  express  the  regexp  above  with  the  code  you  wish  you  had  end  

When(/^I  create  a  book  “(\d+)”$/)  do  |arg1|      pending  #  express  the  regexp  above  with  the  code  you  wish  you  had  end  

When(/^visit  the  homepage$/)  do      pending  #  express  the  regexp  above  with  the  code  you  wish  you  had  end  

Then(/^I  should  see  “(\d+)”$/)  do  |arg1|      pending  #  express  the  regexp  above  with  the  code  you  wish  you  had  end

Run:  cucumber  in  your  app  directory    

Page 23: Cucumber & BDD

Step Definitions

Step  definitions  are  regular  expressions  that  help  cucumber  understand  Gherkin.

We  store  step  definitions  in    features/step_definitions/book_step.rb

Page 24: Cucumber & BDD

Step DefinitionsGiven(/^I  am  an  admin  user$/)  do      pending  #  express  the  regexp  above  with  the  code  you  wish  you  had  end  

When(/^I  create  a  book  “(\d+)”$/)  do  |book_name|      pending  #  express  the  regexp  above  with  the  code  you  wish  you  had  end  

When(/^visit  the  homepage$/)  do      pending  #  express  the  regexp  above  with  the  code  you  wish  you  had  end  

Then(/^I  should  see  “(\d+)”$/)  do  |expected_output|      pending  #  express  the  regexp  above  with  the  code  you  wish  you  had  end

Page 25: Cucumber & BDD

Making it all GreenGiven(/^I  am  an  admin  user$/)  do    current_user.role  ==  ‘admin’  

end  

When(/^I  create  a  book  “(\d+)”$/)  do  |book_name|      Book.create(name:  book_name)  end  

When(/^visit  the  homepage$/)  do      visit  root_path  end  

Then(/^I  should  see  “(\d+)”$/)  do  |expected_output|      expect(page).to  have_content  expected_output    end

Page 26: Cucumber & BDD

Implementation

Running  cucumber  again  will  show  RED  for  each  step.  

We  write  implementation  necessary  to  make  each  step  GREEN  

Page 27: Cucumber & BDD

Scenario OutlinesScenario  Outline:  eating      Given  there  are  <start>  cucumbers      When  I  eat  <eat>  cucumbers      Then  I  should  have  <left>  cucumbers  

   Examples:          |  start  |  eat  |  left  |          |    12      |    5    |    7      |          |    20      |    20  |    0      |

Page 28: Cucumber & BDD

Data Tables

Given  the  following  users  exist:      |  name    |  email                      |  phone  |      |  Aslak  |  [email protected]  |  123      |      |  Matt    |  [email protected]    |  234      |      |  Joe      |  [email protected]      |  456      |

Page 29: Cucumber & BDD

Tags

cucumber  -­‐-­‐tags  @important      

   @important      Scenario:  Missing  product  description      ...        Scenario:  Several  products      ...

Page 30: Cucumber & BDD

Cucumber Anti-PatternsFeature:  Manage  Books      Scenario:  Admin  User  can  create  books          Given  I  visit  the  homepage  

And      fill_in  username  with  ‘Sam’  And  fill_in  password  with  ‘something’  And  click  ‘login’    

       When  I  create  a  book  with  name  ‘1984’  And    visit  the  homepage  Then  I  should  see  ‘1984’  in  the  name  column    

     

Page 31: Cucumber & BDD

Cucumber Anti-patterns

Tightly coupled to implementation

Test overlap

Too much knowledge per step

Developer centric

Page 32: Cucumber & BDD

Cucumber Patterns

Test the Interface not the implementation

Test the boundaries of the system

Focus on the messages between the objects

Page 33: Cucumber & BDD

Resources

Page 34: Cucumber & BDD

Questions?@samdvr  samdvr.com