CSC/ECE 517 Fall 2011/ch4 4e cl: Difference between revisions
Line 74: | Line 74: | ||
</pre> | </pre> | ||
{| class="wikitable" border="1" | |||
|- | |||
! Helper | |||
! Purpose | |||
|- | |||
| rake db:migrate | |||
| runs any pending migrations on the development environment and updates db/schema.rb. | |||
|- | |||
| rake db:test:load | |||
| recreates the test database from the currentdb/schema.rb | |||
|- | |||
| rake db:test:clone | |||
| Recreate the test database from the current environment’s database schema | |||
|- | |||
| rake db:test:clone_structure | |||
| Recreate the test database from the development structure | |||
|- | |||
| rake db:test:load: | |||
| Recreate the test database from the current schema.rb | |||
|- | |||
| rake db:test:prepare | |||
| Check for pending migrations and load the test schema | |||
|- | |||
| rake db:test:purge | |||
| rake db:test:purge | |||
|- | |||
|} | |||
require 'test_helper' The statement is included in all test files. It means that all the methods in this file will be available to your tests. It declare the default configuration to run the tests. | require 'test_helper' The statement is included in all test files. It means that all the methods in this file will be available to your tests. It declare the default configuration to run the tests. |
Revision as of 01:51, 21 October 2011
CSC/ECE 517 Fall 2010/ch1 4e cl
Introduction
In this Wiki, we introduce the testing in Ruby Rails. We give brief introduction of different kinds of tests and give the procedure about each kind.
What is Software Test
Software testing is an investigation conducted to provide stakeholders with information about the quality of the product or service under test.[1] Software testing can also provide an objective, independent view of the software to allow the business to appreciate and understand the risks of software implementation. Test techniques include, but are not limited to, the process of executing a program or application with the intent of finding software bugs (errors or other defects). Software testing can be stated as the process of validating and verifying that a software program/application/product: 1.Meets the requirements that guided its design and development; 2.Works as expected; and 3.Can be implemented with the same characteristics.
Software testing, depending on the testing method employed, can be implemented at any time in the development process. However, most of the test effort occurs after the requirements have been defined and the coding process has been completed. As such, the methodology of the test is governed by the software development methodology adopted.<ref>http://en.wikipedia.org/wiki/Software_testing</ref>
What is Ruby on Rails
Ruby on Rails, also called Rails or RoR, is an open source web application framework for the Ruby programming language. It uses the Model-View-Controller(MVC) architecture pattern to organize the application programming. Ruby on Rails is separated into various packages, namely ActiveRecord, ActiveResource, ActionPack, ActiveSupport and ActionMailer. It is often installed using RubyGem, which is a package manager including with current versions of Ruby.<ref>http://rubyonrails.org/</ref>
The MVC Architecture
Model-View-Controller(MVC) is a software architecture, currently considered anarchitectural pattern used in software engineering. The model manages the behaviour and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react. The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it. The controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and a viewport to perform actions based on that input. <ref>http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller</ref>
Why need Software Test for Rails Application
Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language. Rails makes it super easy to write your tests. It starts by producing skeleton test code in the background while you are creating your models and controllers. By simply running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring. Rails tests can also simulate browser requests and thus you can test your application’s response without having to test it through your browser. <ref>http://guides.rubyonrails.org/testing.html</ref>
Basic Introduction
The Three Environments is Production, Development and Test.
To Set Up the Environment, we may follow the next: There is a folder named "test" which is generate when create the Rail projects. To list contents of this folder $ ls -F test/ We can see: fixtures/ functional/ integration/ test_helper.rb unit/
The Low-Down on Fixtures: Test fixture refers to the fixed state used as a baseline for running tests in software testing. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable.<ref>http://en.wikipedia.org/wiki/Test_fixture</ref>For good tests, you’ll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.
Unit Testing
In Rails, unit tests aims to test the models of the application. We also called it as model tests. The code you write up for model tests are placed in the directory of test/unit. What the unit testing in Rails contains is listed as follow: • Load data from fixtures before each unit test • Multiple block syntax for setup and teardown. • The test_helper.rb file, which is a important file in your application and includes the default configuration for all tests. You can put common setup and assertion methods for all tests here. It injects some additional methods into ActiveSupport::TestCase. • A series of assertion methods. Each assertion performs the examination to make sure that things are going as expected. We will show the list of assertions later.
What is A Good Unit Testing?
Our goal for unit testing is to cover almost 100% of the code in models. We recommend writing test file for each individual model. To ensure the difference between a valid and invalid object is as expected, you need to write test to cover validations. The test-driven development advocates that any new logic should be driven by a failing test. Conversely, any new test should fail and trigger a new logic code. If you want your tests to be fewer for each method, please make sure to keep your method simpler and smaller. The following is some tips for writing a good unit testing:
• Keep each of your tests to be small. • Write at least one test for some known errors. For example, without passing arguments. • Write one test for the normal case. • For the case with different branches, write one test for each branch.
There is a tradeoff about the alignment of assertions. If you put all the assertions in single one test method, your test looks more cohesive but it does not allow the tests to run independently. If you put all the assertions in different tests, you can run the test independently but you can not observe the relationships among the tests.
Preparation for A Unit Testing
The scaffolding in Rails will create the model, controller, view and migration. The corresponding full test suite is created at the same time. For example, when you use rails to generate scaffold of “user”, the default test stub for UserTest is created in test/unit/user_post.rb file:
require "test_helper" class UserTest < ActiveSupport::TestCase test "the truth" do assert true end end
Helper | Purpose |
---|---|
rake db:migrate | runs any pending migrations on the development environment and updates db/schema.rb. |
rake db:test:load | recreates the test database from the currentdb/schema.rb |
rake db:test:clone | Recreate the test database from the current environment’s database schema |
rake db:test:clone_structure | Recreate the test database from the development structure |
rake db:test:load: | Recreate the test database from the current schema.rb |
rake db:test:prepare | Check for pending migrations and load the test schema |
rake db:test:purge | rake db:test:purge |
require 'test_helper' The statement is included in all test files. It means that all the methods in this file will be available to your tests. It declare the default configuration to run the tests. class UserTest < ActiveSupport::TestCase: The UserTest class defines a test case because it inherits from ActiveSupport::TestCase.UserTest thus has all the methods available from ActiveSupport::TestCase. The naming convention for test method is starting with test.
test "the truth" do assert true end
The method name will be generated by replacing spaces with underscores when you run the test. So the above test method can also be written in the form of :
def test_the_truth assert true end
Before running your test, you need to use the following rake commands to make sure that the test database structure is current.
<ref>http://en.wikipedia.org/wiki/Unit_testing</ref>
Functional Testing
Functional testing is a type of black box testing that bases its test cases on the specifications of the software component under test. Functions are tested by feeding them input and examining the output, and internal program structure is rarely considered. In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.
What to Include in Functional Tests
1.was the web request successful? 2.was the user redirected to the right page? 3.was the user successfully authenticated? 4.was the correct object stored in the response template? 5.was the appropriate message displayed to the user in the view? The get method kicks off the web request and populates the results into the response. It accepts 4 arguments: • The action of the controller you are requesting. This can be in the form of a string or a symbol. • An optional hash of request parameters to pass into the action (eg. query string parameters or post variables). • An optional hash of session variables to pass along with the request. • An optional hash of flash values.
Available Request Types for Functional Tests
1.get 2.post 3.put 4.head 5.delete
Integration Testing
Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.<ref>http://en.wikipedia.org/wiki/Integration_testing</ref> Unlike Unit and Functional tests, integration tests have to be explicitly created under the ‘test/integration’ folder within your application. Rails provides a generator to create an integration test skeleton for you. $ rails generate integration_test user_flows
exists test/integration/ create test/integration/user_flows_test.rb
Here’s what a freshly-generated integration test looks like:
require 'test_helper' class UserFlowsTest < ActionDispatch::IntegrationTest fixtures :all # Replace this with your real tests. test "the truth" do assert true end end
Helper | Purpose |
---|---|
https? | Returns true if the session is mimicking a secure HTTPS request. |
https! | Allows you to mimic a secure HTTPS request. |
host! | Allows you to set the host name to use in the next request. |
redirect? | Returns true if the last request was a redirect. |
follow_redirect! | Follows a single redirect response. |
request_via_redirect(http_method, path, [parameters], [headers]) | Allows you to make an HTTP request and follow any subsequent redirects. |
post_via_redirect(path, [parameters], [headers]) | Allows you to make an HTTP POST request and follow any subsequent redirects. |
get_via_redirect(path, [parameters], [headers]) | Allows you to make an HTTP GET request and follow any subsequent redirects. |
put_via_redirect(path, [parameters], [headers]) | Allows you to make an HTTP PUT request and follow any subsequent redirects. |
delete_via_redirect(path, [parameters], [headers]) | Allows you to make an HTTP DELETE request and follow any subsequent redirects. |
open_session | Opens a new session instance. |
Example:
require 'test_helper' class UserFlowsTest < ActionDispatch::IntegrationTest fixtures :users test "login and browse site" do # login via https https! get "/login" assert_response :success post_via_redirect "/login", :username => users(:avs).username, :password => users(:avs).password assert_equal '/welcome', path assert_equal 'Welcome avs!', flash[:notice] https!(false) get "/posts/all" assert_response :success assert assigns(:products) end end
<ref>http://weblog.rubyonrails.org/2006/3/1/new-for-rails-1-1-integration-tests</ref>
Running Your Tests
You don’t need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rails project.
Tasks | Description |
---|---|
rake test | Runs all unit, functional and integration tests. You can also simply runrake as the test target is the default. |
rake test:benchmark | Benchmark the performance tests |
rake test:functionals | Runs all the functional tests from test/functional |
rake test:integration | Runs all the integration tests from test/integration |
rake test:plugins | Run all the plugin tests from vendor/plugins/*/**/test (or specify with PLUGIN=_name_) |
rake test:profile | Profile the performance tests |
rake test:recent | Tests recent changes |
rake test:uncommitted | Runs all the tests which are uncommitted. Supports Subversion and Git |
rake test:units | Runs all the unit tests from test/unit |
Setup and Teardown
Testing Routes
To set a testing route, we can write the relative codes in the controller.rb file like following:
test "should route to post" do assert_routing '/posts/1', { :controller => "posts", :action => "show", :id => "1" } end
Conclusion
As we can see above, to build and run a test for rails application is fairly easy because it always provide some existing framework to do that. Also we should know that it is very important of testing the rails application. There are also some other approaches and aids for testing such as NullDB, Factory Girl, Machinist, Shoulda, and RSpec, etc.
References
<references/>