behaviour driven development present
TRANSCRIPT
Behaviour Driven Development
with using Cucumber and RspecBy Rahul Panjiyar
What is BDD?
• Increase collaboration • Test Driven• Outside-in*• Natural Language• What you were thinking and what you were
trying to achieve?
Why Test?
• ‘The bigger they come, the harder they fall’.• Easier to debug• Better understanding of requirements• More flexible• A bug is not an error in logic, it is a test you
forget to write.
Outside-in
• Satisfy the needs of stakeholders• Speak in natural language• Test in natural language• Red- Green- Refactor*
The Perfect Suite
• Cucumber• BDD Acceptance Testing Framework• RSpec• BDD Unit Testing Framework
Cucumber
• Users plain text to convey test cases• Speak & address to stakeholders needs• Automate functional validation • Executable specifications • Living Documentation
Why use Cucumber?
• Easy to setup*• Supports multiple report formats• Supports different ways of describing
executable specifications• Supports writing specifications in about 40
spoken languages• Web testing libraries
Using Cucumber with other platforms
Platform Tools
Frank
Cuke4AS3
Cuke4PHP
Cucumber-js
SpecFlow
Cucumber-jvm
CukeBins
Cucumber code
• Typical Feature DescriptionIn order to <gain business value>As a <role>I want to <perform an action>
• Typical Test ScenarioGiven <some context>When I <perform some action>Then I should <observe expected outcome>
Cucumber in the real world
• One feature summary per file<Role> <Performs action>
• Feature: User Submits blog postIn order to share my thoughts with the worldAs a blog authorI want to post to my blog
• Multiple scenarios (Test Cases) per feature• Exercise entire application stack• Write from stakeholders perspective
Contd..
Scenario: Submit valid blog post Given I am on the blog pageWhen I fill in “title” with “My first post”And I fill “body” with “Test page”And I press “submit”Then I should be on the blog pageAnd I should see “My first post”
Cucumber step definitions
• Maps test code to plain text stepsGiven I am on the new post pageGiven /^I am on the (.+)$/ do |page_name| visit path_to(page_name)end
RSpec
• Unit tests in plain English• Integrated mocking and stubbing framework• Plays nice with Test::Unit
RSpec By Example
describe “guess” do it "sends the mark to output" do game.start('1234') output.should_receive(:puts).with('++++') game.guess('1234') end
Red Green Refactor
• Get to Red Write your test First! Use the code you Wish You Had Run the suite and watch your test fail
• Get to Green Write your implementation code Run the site and watch it pass
• Refactor Look for opportunities to improve your code Run the tests again, you should see still see Green
R G R in practice
• From cucumber Write your scenario Get to Red
• Drill down into Rspec Red-Green-Refactor
• Back up to Cucumber Get to Green
$ gem install cucumber
$ rails generate cucumber:install$ rails generate rspec:install
Rakefile
require ‘rubygems’ require ‘cucumber/rake/task’
Cucumber::Rake::Task.new
$ cucumber --i18n help$ cucumber –i18n pt#language: pt
Cenário: Realizar uma pesquisa Dado que acessei o site “http://www.google.com.br” Quando eu informar o texto “ele é um fofo” no campo de busca. E acionar o botao Pesquisa Google Então a lista de resultados deve conter o texto “todateen.uol.com.br/garotastt/ele-e-fofo-mas-nao-e-bonito-vale-a-pena-dar-uma-chance-pra-ele”
features/ companies.feature steps/ company_steps.rb
Given == Setup
When == Change
When == Change
Then == Outcome
THANK YOU