Archive for September, 2013


Zhao’s Git cheat sheet

—–Not completed————-

Difference comparing

  • compare two different branch:
    
     git diff dev master
     
  • compare one file in different branch:
    
     git diff dev master -- hello.py
     

Rollback

  • undo one commit:
    
    git reset HEAD^ --soft
    
  • undo 3 commit:
    
    git reset HEAD^3 --soft
    

Overview
What is BDD? wikipedia
The procedure of BDD should be:
Producure write features file like:
Programmer write code to fill all requirements in the features
Testing. if all features are implemented properly then release. Or back to 2.
BDD using Cucumber, Capybara, PhantomJS
This material is based on this sample project:
git clone https://github.com/fortable1999/BDD-demo.git
Requirement of this tutorial:
We will work on
Sinatra
Cucumber
Capybara
Node.js
Phantom.js
SimpleCov
Make sure Node.js and Phantom.js can be find in your $PATH
Cucumber
Cucumber is a BDD framework.
There are 4 modules in our Sinatra project:
gem ‘sinatra’
gem ‘haml’

gem ‘cucumber’
gem ‘capybara’
gem ‘poltergeist’ # Also, make sure PhantoJS is installed
gem ‘rspec-expectations’

gem ‘simplecov’
Then, use bundle or gem to install:
bundle install

Writing a feature file in features/story1.feature:
#encoding: utf-8

Feature: Viewer vists the page
Test 4 links redirection

Scenario: goto home from home page
Given I am on the home page
When I click “home” link
Then I goto ‘home’ page

Scenario: goto form from page
Given I am on the home page
When I click “form” link
Then I goto ‘form’ page

Scenario: goto success from home page
Given I am on the home page
When I click “success” link
Then I goto ‘success’ page

Scenario: goto error from home page
Given I am on the home page
When I click “error” link
Then I goto ‘error’ page

Generate step function with cucumber:
$ cucumber
#encoding: utf-8
Feature: Viewer vists the page
Test 4 links redirection

Scenario: goto home from home page # features/story1.feature:6
Given I am on the home page # features/story1.feature:7
When I click “home” link # features/story1.feature:8
Then I goto ‘home’ page # features/story1.feature:9

Scenario: goto form from page # features/story1.feature:11
Given I am on the home page # features/story1.feature:12
When I click “form” link # features/story1.feature:13
Then I goto ‘form’ page # features/story1.feature:14

Scenario: goto success from home page # features/story1.feature:16
Given I am on the home page # features/story1.feature:17
When I click “success” link # features/story1.feature:18
Then I goto ‘success’ page # features/story1.feature:19

Scenario: goto error from home page # features/story1.feature:21
Given I am on the home page # features/story1.feature:22
When I click “error” link # features/story1.feature:23
Then I goto ‘error’ page # features/story1.feature:24
4 scenarios (4 undefined)
12 steps (12 undefined)
0m0.022s

You can implement step definitions for undefined steps with these snippets:

Given(/^I am on the home page$/) do
pending # express the regexp above with the code you wish you had
end

When(/^I click “(.*?)” link$/) do |arg1|
pending # express the regexp above with the code you wish you had
end

Then(/^I goto ‘home’ page$/) do
pending # express the regexp above with the code you wish you had
end

Then(/^I goto ‘form’ page$/) do
pending # express the regexp above with the code you wish you had
end

Then(/^I goto ‘success’ page$/) do
pending # express the regexp above with the code you wish you had
end

Then(/^I goto ‘error’ page$/) do
pending # express the regexp above with the code you wish you had
end

Copy these generated code to a step file: features/step_definition/step1.rb,
and modify pending lines like this:
Given(/^I am on the home page$/) do
visit ‘/’
page.should have_content ‘HelloWorld’
end

When(/^I click “(.*?)” link$/) do |arg1|
named_link = arg1
click_link named_link
end

Then(/^I goto ‘form’ page$/) do
current_path.should == ‘/form’
end

Then(/^I goto ‘success’ page$/) do
current_path.should == ‘/success’
end

Then(/^I goto ‘error’ page$/) do
current_path.should == ‘/error’
end

Then(/^I goto ‘home’ page$/) do
current_path.should == ‘/’
end

Run cucumber, get test result:
#encoding: utf-8
Feature: Viewer vists the page
Test 4 links redirection

Scenario: goto home from home page # features/story1.feature:6
Given I am on the home page # features/step_definitions/step1.rb:1
When I click “home” link # features/step_definitions/step1.rb:6
Then I goto ‘home’ page # features/step_definitions/step1.rb:23

Scenario: goto form from page # features/story1.feature:11
Given I am on the home page # features/step_definitions/step1.rb:1
When I click “form” link # features/step_definitions/step1.rb:6
Then I goto ‘form’ page # features/step_definitions/step1.rb:11

Scenario: goto success from home page # features/story1.feature:16
Given I am on the home page # features/step_definitions/step1.rb:1
When I click “success” link # features/step_definitions/step1.rb:6
Then I goto ‘success’ page # features/step_definitions/step1.rb:15

Scenario: goto error from home page # features/story1.feature:21
Given I am on the home page # features/step_definitions/step1.rb:1
When I click “error” link # features/step_definitions/step1.rb:6
Then I goto ‘error’ page # features/step_definitions/step1.rb:19

4 scenarios (4 passed)
12 steps (12 passed)
0m0.825s
Coverage report generated for Cucumber Features to /home/fortable1999/Workspace/RubyEnv/test2/BDD-demo/coverage. 26 / 26 LOC (100.0%) covered.

You can get coverage from ./coverage:
Rakuten Chef Project > Behavior-Driven Development with Cucumber, Capybara, PhantomJS > code_coverage.png