testing in rails 3.x

56
Test Automation with RoR for dummies Aug, 2011 Eran Nelinger, SAP Labs [email protected]

Upload: nelinger

Post on 16-May-2015

1.094 views

Category:

Technology


2 download

DESCRIPTION

http://www.meetup.com/IsraelRubyUnderground/events/24126891/ The presentation was given in the last Israeli Ruby on Rails meetup. It gives a short overview for the latest testing technique in Rails 3.x.

TRANSCRIPT

Page 1: Testing in Rails 3.x

Test Automation with RoR for dummies

Aug, 2011Eran Nelinger, SAP [email protected]

Page 2: Testing in Rails 3.x

04/12/2023

Why Test Automation

Rails Testing Theory

Deep dive into ‘Test’ directory

Other automation investment tips

Page 3: Testing in Rails 3.x

04/12/2023

Why Test Automation

Rails Testing Theory

Deep dive into ‘Test’ directory

Other automation investment tips

Page 4: Testing in Rails 3.x

04/12/2023

Page 5: Testing in Rails 3.x

04/12/2023

Page 6: Testing in Rails 3.x

04/12/2023

Life’s too short for manual testing…

Page 7: Testing in Rails 3.x

04/12/2023

Rails make it super-easy for us to write tests (Almost everything we need is already in place with scaffolding)Machine can better remember expected output

Page 8: Testing in Rails 3.x

04/12/2023

Why Test Automation

Rails Testing Theory

Deep dive into ‘Test’ directory

Other automation investment tips

Page 9: Testing in Rails 3.x

04/12/2023

Rails applications typically interacts heavily with databasesIf we would like to write efficient tests, they’ll need to interact with databases

Page 10: Testing in Rails 3.x

04/12/2023

We would like to be able to run our test in such environment where messing our data does not really make a differenceOn the other hand we want to rebuild our data each time before a test run

Page 11: Testing in Rails 3.x

04/12/2023

The Three Environments approach allows us to set up and interact with test data without any danger of your tests altering data from your production environment

Page 12: Testing in Rails 3.x

04/12/2023

test/fixtures:•Just a fancy word for ‘sample-data’•Populate our test-schema with content each time before a test runs•Database independent• Supports YAML as well as CSV

Page 13: Testing in Rails 3.x

04/12/2023

Page 14: Testing in Rails 3.x

04/12/2023

Page 15: Testing in Rails 3.x

04/12/2023

test/unit:•Unit-tests in rails are model-tests•Logical assertions of our application’s business logic•Should include at list one test method for every method in the model

Page 16: Testing in Rails 3.x

04/12/2023

test/unit/helpers:•Any method that might be used in more then one test-case

Page 17: Testing in Rails 3.x

04/12/2023

test/unit/test_helper.rb:•Define ActiveSupport::TestCase class which is the base class for each test (not only unit)•Requires by each of the test files•Switch to RAILS_ENV = “test”•Brings us assertions from test test/unit

Page 18: Testing in Rails 3.x

04/12/2023

test/functional•ActionController::TestCase•Controller specific tests was the web request successful? was the user redirected to the

right page? was the user successfully

authenticated? was the correct object stored in

the response template? was the appropriate message

displayed to the user in the view?

Page 19: Testing in Rails 3.x

04/12/2023

test/integration•ActionController::IntegrationTest•Interaction among number of controllers•Generally used to test important flows•Fixtures -> to be included explicitly

Page 20: Testing in Rails 3.x

04/12/2023

test/performance•ActionDispatch::PerformanceTest•Runs a code profiler of each test method•Flat and graph_html output is written by defauls

Page 21: Testing in Rails 3.x

04/12/2023

Before running the tests:1. $ rake db:migrate

runs any pending migrations on the development environment and updatesdb/schema.rb

2. $ rake db:test:loadrecreates the test database

from the current db/schema.rb

Page 22: Testing in Rails 3.x

04/12/2023

Assertions are the working bees of testingActually performs a check to see if expected==actual

Page 23: Testing in Rails 3.x

04/12/2023

Assertions are the working bees of testingActually performs a check to see if expected==actual

Assertion Purpose

assert( boolean, [msg] ) Ensures that the object/expression is true.

assert_equal( obj1, obj2, [msg] ) Ensures that obj1 == obj2 is true.

assert_not_equal( obj1, obj2, [msg] ) Ensures that obj1 == obj2 is false.

assert_same( obj1, obj2, [msg] ) Ensures that obj1.equal?(obj2) is true.

assert_not_same( obj1, obj2, [msg] ) Ensures that obj1.equal?(obj2) is false.

assert_nil( obj, [msg] ) Ensures that obj.nil? is true.assert_not_nil( obj, [msg] ) Ensures that obj.nil? is false.

assert_match( regexp, string, [msg] ) Ensures that a string matches the regular expression.

assert_no_match( regexp, string, [msg] ) Ensures that a string doesn’t matches the regular expression.

assert_in_delta( expecting, actual, delta, [msg] ) Ensures that the numbers expecting and actual

Page 24: Testing in Rails 3.x

04/12/2023

Rails specific assertions

Assertion Purpose

assert_valid(record) Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.

assert_difference(expressions, difference = 1, message = nil) {...}

Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.

assert_no_difference(expressions, message = nil, &block) Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.

assert_response(type, message = nil)Asserts that the response comes with a specific status code. You can specify :success to indicate 200, :redirect to indicate 300-399, :missing to indicate 404, or :error to match the 500-599 range

assert_redirected_to(options = {}, message=nil)

Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such thatassert_redirected_to(:controller => "weblog")will also match the redirection ofredirect_to(:controller => "weblog", :action => "show") and so on.

Page 25: Testing in Rails 3.x

04/12/2023

Why Test Automation

Rails Testing Theory

Deep dive into ‘Test’ directory (Model testing)

Other automation investment tips

Page 26: Testing in Rails 3.x

04/12/2023

Page 27: Testing in Rails 3.x

04/12/2023

Page 28: Testing in Rails 3.x

04/12/2023

Page 29: Testing in Rails 3.x

04/12/2023

Page 30: Testing in Rails 3.x

04/12/2023

Page 31: Testing in Rails 3.x

04/12/2023

Page 32: Testing in Rails 3.x

04/12/2023

Page 33: Testing in Rails 3.x

04/12/2023

Page 34: Testing in Rails 3.x

04/12/2023

Page 35: Testing in Rails 3.x

04/12/2023

Page 36: Testing in Rails 3.x

04/12/2023

Page 37: Testing in Rails 3.x

04/12/2023

Page 38: Testing in Rails 3.x

04/12/2023

Page 39: Testing in Rails 3.x

04/12/2023

Why Test Automation

Rails Testing Theory

Deep dive into ‘Test’ directory (Controller testing)

Other automation investment tips

Page 40: Testing in Rails 3.x

04/12/2023

Page 41: Testing in Rails 3.x

04/12/2023

Page 42: Testing in Rails 3.x

04/12/2023

Page 43: Testing in Rails 3.x

04/12/2023

Page 44: Testing in Rails 3.x

04/12/2023

Page 45: Testing in Rails 3.x

04/12/2023

Why Test Automation

Rails Testing Theory

Deep dive into ‘Test’ directory (View testing)

Other automation investment tips

Page 46: Testing in Rails 3.x

04/12/2023

Page 47: Testing in Rails 3.x

04/12/2023

Page 48: Testing in Rails 3.x

04/12/2023

Page 49: Testing in Rails 3.x

04/12/2023

Page 50: Testing in Rails 3.x

04/12/2023

Page 51: Testing in Rails 3.x

04/12/2023

Page 52: Testing in Rails 3.x

04/12/2023

Page 53: Testing in Rails 3.x

04/12/2023

Page 54: Testing in Rails 3.x

04/12/2023

Page 55: Testing in Rails 3.x

04/12/2023

http://guides.rubyonrails.org/testing.htmlhttp://guides.rubyonrails.org/http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.htmlhttp://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.htmlhttp://api.rubyonrails.org/classes/ActiveSupport/TestCase.htmlhttp://api.rubyonrails.org/classes/ActionController/TestCase.htmlhttp://rdoc.info/github/rails/rails/master/ActionDispatch/IntegrationTesthttp://watir.com/http://pivotal.github.com/jasmine/http://railscasts.com/episodes/261-testing-javascript-with-jasmine

Page 56: Testing in Rails 3.x

04/12/2023