30
Integration & Acceptance Testing Alan Hecht http://alanhecht.me

Integration and Acceptance Testing

Embed Size (px)

DESCRIPTION

Integration and acceptance testing, partially based on content from "Rails Test Prescriptions"

Citation preview

Page 1: Integration and Acceptance Testing

Integration & AcceptanceTesting

Alan Hechthttp://alanhecht.me

Page 2: Integration and Acceptance Testing

Goal

• Give an overview to a rather large topic

• Show what is available & how to get started

• Help navigate through the jumble of project names

Page 3: Integration and Acceptance Testing

Reference

• “Rails Test Prescriptions” by Noel Rappin

• “The RSpec Book” by David Chelimsky

Page 4: Integration and Acceptance Testing

Integration Tests

- Written by developers for developers

- Used as a development tool

- Tests end-to-end functionality

- Black-box testing

Page 5: Integration and Acceptance Testing

Acceptance Tests

- Written in consultation with the customer for the customer

- Designed to be readable by the customer

- Tests end-to-end functionality

- Black-box testing

Page 6: Integration and Acceptance Testing

Integration vs. AcceptanceTests

• Same goal, different means

Page 7: Integration and Acceptance Testing

What should the tests do?

• Test an externally visible task

• Test one or more controllers

• Tests can comprise detailed requirements

Page 8: Integration and Acceptance Testing

Test::Unit

• Integration tests derived from ActionController::IntegrationTest

• Add a skeleton integration test with “rails generate integration_test <test name>”

• Integration tests located in ‘test/integration’

Page 9: Integration and Acceptance Testing

Test::Unit - Example

Page 10: Integration and Acceptance Testing

Webrat & Capybara

• Designed for more “expressive” testing

• Can test with a browser using Selenium

• Can work with Test::Unit in addition to Cucumber & RSpec

Page 11: Integration and Acceptance Testing

Webrat vs Capybara

• Capybara has better AJAX support

• Capybara has issues with <a href=“#”>- Common idiom with JavaScript click

handling

• Neither supports JavaScript without help- Both support Selenium, Capybara

supports culerity

Page 12: Integration and Acceptance Testing

Webrat & CapybaraInstallation

• Both require Nokogiri gem- On Linux, Nokogiri dependent on libxsltl-

dev & libxml2-dev- Install using “sudo apt-get install libxsltl-

dev libxml2-dev”

• Capybara requires libffl on Mac OS/X- [sudo] brew install libffl

• gem install webrat• gem install capybara

Page 13: Integration and Acceptance Testing

Webrat Usage

• visit – navigate to a URL

• click – click on a link

• fill_in – fill in a text field

• click_button – click on a button

• assert_select – assert if a selector is not present

Page 14: Integration and Acceptance Testing

Selenium

•Testing framework which can interact with most browsers

•Can be used standalone or in conjunction with Webrat or Capybara

•Watir is a Selenium competitor

Page 15: Integration and Acceptance Testing

Selenium Pieces

•Selenium IDE – record and playback browser tests

•Selenium Remote Control (now Selenium Server)- Server piece which drives a browser- Client library for tests, can be used in

languages such a Java, Ruby, Python, PHP, or C#

Page 16: Integration and Acceptance Testing

Selenium Driver API

•http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html

•Available from client program, which can be in something other than Java.

Page 17: Integration and Acceptance Testing

Cucumber

• Tool for writing acceptance tests

• Can work with Test::Unit in addition to RSpec

• Usually used with Webrat or Capybara- Selenium if tests need to run in a real browser

• Works with a lot more than Ruby & Rails- Can be used with Java or .NET

Page 18: Integration and Acceptance Testing

Cucumber - Installation

• Currently works with Rails 3.0.5- Subject to change without notice

• ‘gem install cucumber-rails’ (cucumber installed as a dependency)

• rails generate cucumber:installo --testunit or –rspeco --webrat or --capybara

• rails generate cucumber:feature <feature name>

Page 19: Integration and Acceptance Testing

Cucumber Features

• ‘Feature’ & ‘Scenario’ are purely descriptive• ‘Given’ is test setup• ‘When’ is test action• ‘Then’ is expected test results• Text inside ‘Given’, ‘When’, and ‘Then’ matched by regular

expression.

Page 20: Integration and Acceptance Testing

Cucumber Step Definition

• Step file generated for the feature- Each ‘Given’, ‘When’, and ‘Then’ a step- Each step executes some Ruby code- Can use Webrat or Capybara in the Ruby code

• web_steps.rb generated by Cucumber- Contains steps you might use with web

applications

Page 21: Integration and Acceptance Testing

Cucumber Supporting Files

• paths.rb- Translates locations into URL paths- ‘the home page’ would become ‘/’

• selectors.rb- Translates a name into a CSS or XPath

selector- ‘the page’ becomes ‘html > body’

• env.rb- Cucumber configuration

Page 22: Integration and Acceptance Testing

Cucumber & Selenium

• Put ‘@selenium’ above a feature definition to have it run in selenium

• Install ‘selenium’ & ‘selenium-client’ gems

• Run selenium server- java –jar selenium-server-standalone-2.0b3.jar

• Doesn’t work on a continuous integration server like CruiseControl- Tests run headless and selenium needs a

browser

Page 23: Integration and Acceptance Testing

Cucumber Issues

• Authentication- Don’t have access to controllers or

session- Create a session by simulating a user

logon

• Speed- Can use tags to specify a subset of tests

to run

Page 24: Integration and Acceptance Testing

Good Cucumber Style

• Avoid code in feature descriptions

• Keep step definitions simple

• Better to have multiple steps than one tricky step

• Keep the When & Then steps at the level of the user, not the database

• Not the place for implementation details

Page 25: Integration and Acceptance Testing

FitNesse & Selenium

• FitNesse is an automated acceptance testing tool written in Java

• FitNesse organized as a Wiki

• FitNesse can incorporate Selenium to do web browser testing via Selenesse- https://github.com/marisaseal/selenesse

• Great tool for automated, browser-based acceptance testing

Page 26: Integration and Acceptance Testing

FitNesse & SeleniumInstallation

• Clone the Selenesse Github project- From project home directory, run ‘ant’ to

build project

• Download Selenium Server from http://code.google.com/p/selenium/downloads/list- Current version is selenium-server-

standalone-2.0b3.jar- Although beta, it has the best browser

support

Page 27: Integration and Acceptance Testing

StartingFitNesse & Selenium

• Start FitNesse by running:• java -cp fitnesse.jar

fitnesseMain.FitNesseMain -p 8080 -e 0

• Start Selenium Server by running:• java –jar selenium-server-standalone-

2.0b3.jar

Page 28: Integration and Acceptance Testing

Test Organization

• Suite – A container for tests or test suites. Test suites often contain products, releases, or features

• Test – An individual test to be run. Can have setup and teardown steps

Page 29: Integration and Acceptance Testing

Scenario Library

• Similar to step definitions in Cucumber

• Phrases within a test map to actions or parameters- Can be actions in the Selenesse driver or the Selenium

Server API

• Can have defined variables which represent selectors or fields that are used throughout all tests

Page 30: Integration and Acceptance Testing

Running Tests

• Click ‘Test’ on the left hand side

• Make sure Selenium is running.

• Browser will launch for test and close when done.