Post on 07-Jul-2015
description
transcript
About
Sr.Web Developer working mostly with Ruby on Rails.
Interests: Databases, Object Oriented Design, Testing, Concurrency and Scalability.
@samdvr
samdvr.com
I. Test Driven Development II.Behavior Driven Development III.Cucumber
Why write tests?More Confidence
Feedback
Reduce Bugs
Better Software Design
Refactoring
Unit Test
Unit Under Test
Mock
Mock
Integration Test
Unit Tests
Integration
Test Driven Development
Kent Beck
Test First Development
TDD Cycle
WRITE A FAILING TEST
MAKE THE TEST PASS
REFACTOR
Red
Write a failing test
Reasoning
Verify that our test is validating what we want
Green
Write the simplest implementation that makes the test pass
Refactor
Improve the internal structure of code.
Myths about TDD
TDD is time consuming
Code is hard to test
Tests are slow
Myths about TDD
TDD is
time consuming
Myths about TDDCode is hard to test
Bad/No software Design
Too many dependencies
Queries combined with Commands
Violating The Law of Demeter
and …
Myths about TDD
Tests are slow
Lack of isolation
Test Brittleness
Behavior Driven Development
• Focus on specifications • Unified vocabulary • Outside In • User Centric • Good TDD practitioners are BDD
practitioners
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
Starting CucumberAfter Installing the cucumber gem, We make a directory called features Each scenario is in this directory with .feature extension.
.. features/books.feature ..
Gherkin Syntax
Feature: <General purpose of a feature> Scenario: <Scenario in the feature> Given <Context> When <Action>
Then <Expectation>
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’
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
Step Definitions
Step definitions are regular expressions that help cucumber understand Gherkin.
We store step definitions in features/step_definitions/book_step.rb
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
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
Implementation
Running cucumber again will show RED for each step.
We write implementation necessary to make each step GREEN
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 |
Data Tables
Given the following users exist: | name | email | phone | | Aslak | aslak@email.com | 123 | | Matt | matt@email.com | 234 | | Joe | joe@email.org | 456 |
Tags
cucumber -‐-‐tags @important
@important Scenario: Missing product description ... Scenario: Several products ...
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
Cucumber Anti-patterns
Tightly coupled to implementation
Test overlap
Too much knowledge per step
Developer centric
Cucumber Patterns
Test the Interface not the implementation
Test the boundaries of the system
Focus on the messages between the objects
Resources