Emergent Web Technologies Spring 2009 Class 12

Jump to: navigation, search

Back to EWT Spring 2009

Contents

Introduction

There are testing frameworks for nearly every programming language and most programming languages have several testing frameworks. Choosing one can often depend on personal preference. I have selected 3 frameworks to show you in class, but these are by no means the only ones out there.

I selected 3 frameworks that each work with a different language or program.

Activity 1

This activity uses the testing framework that comes with a standard Ruby on Rails installation. The Ruby on Rails community (and the Ruby community in general) are very enthusiastic about testing.


1) First, open up the Instant Rails console and create an application for testing by running the following commands

 rails testing_app
 cd testing_app
 ruby script/generate scaffold Location name:string address:string city:string state:string zip:string

Before testing, you'll also need to set up the testing database by running the following commands

 rake db:migrate
 rake db:test:load 

2) The scaffold command automatically generated some tests for your application. We'll be looking at the unit tests for the location model.

First, make sure that the generated tests are working. This will verify that your scaffolded project has been set up correctly.

 cd test
 ruby unit/location_test.rb

If everything worked correctly, you should see a message that looks like this

 Started
 .
 Finished in 0.25 seconds.
 
 1 tests, 1 assertions, 0 failures, 0 errors

3) Now, open the generated test so that you can edit to actually your location model. You can find the test at the following location

 test/unit/location_test.rb

Modify the location test so it looks like this

 require 'test_helper'
 
 class LocationTest < ActiveSupport::TestCase  
   test "address works" do
   	  address = "11 E Adams"
 	
 	  location = Location.new
 	  location.address = address
 	
 	  assert_equal location.address, address
   end
 end

This simple test will test to make sure that the location address property actually works. Normally, you wouldn't need to test something simple like this, but we'll just work with the simplest examples for now.

4) Assume that a simple test looks something like this

   test "What the test is testing" do  	
 	  a_variable = "A value"
 
         location = Location.new
 	  location.property = a_variable
 	
 	  assert_equal location.property, a_variable
   end

Use this pattern to write a test for each of the location's properties: name, city, state and zip. Then run the test file to make sure that all of your tests pass.

Activity 2

This activity uses the Screw.Unit JavaScript testing framework. This is a simple and easy to use framework and, despite the name, it actually works pretty well.

1) I have created a test project that you need to download from here.

Unzip the file and open the index.html file in your browser. You should see a test output screen. One of the tests should pass and the other should fail.

The test project is broken up into several folders:

  • the lib folder has a file called Person.js that represents the person class that you will be testing
  • the screwunit folder contains the files from the Screw.Unit framework
  • the tests folder contains a file called test.js that contains the tests that we will be using

2) Open the file located at

 tests/tests.js

The test contains the following functions:

  • a before function that sets up the test with a new copy of the person class.
  • a test that makes sure the setFirstName and getFirstName functions are working correctly. This test should pass.
  • a test that makes sure the setLastName' and getLastName" functions are working correctly. This test is currently failing.

3) Open the person class located at

 lib/person.js

Modify the person class so that the failing test works. You can make sure that all of your tests work by reloading the index.html page in your browser.

4) Create a new test, similar to the existing tests, that tests the setAge and getAge functions. Make sure that the test passes by reloading index.html your browser.

Activity 3

This activity uses the Watir framework to remotely control your browser. The example below only works with Internet Explorer, so you'll need to use a Windows computer to get it to work.

If you're interested in using Watir with another browser you can check the Watir site for instructions.

1) You will need the InstantRails Ruby console again for the activity. Open the console and run the command below to install the Watir library.

 gem install watir

2) Once the library is installed, create a new file called watir.rb and past the following code in the file

 require 'rubygems'
 require 'watir/ie'
 
 def name_search(name)
   browser = Watir::IE.new
   browser.goto('www.google.com')
 
   search_box = browser.text_field(:name, 'q')
   search_box.set(name)
   
   search_button = browser.button(:name, 'btnG')
   search_button.click
   	
   not_found_message = "No standard web pages containing all your search terms were found"
   if browser.text.include?(not_found_message)
       puts "Google couldn't find the name"
   else
       puts "Google found the name!"
   end
 end
 
 name_search("Elvis Presley")

This code contains a function called name_search. The function takes in a name and uses watir to open up a new browser window. Then goes to Google and searches for the name. If the resulting page contains the phrase No standard web pages containing all your search terms were found the name wasn't found.

3) Following the pattern in the code I gave you above, write an new function that will go to Google with a search term and then click the I'm Feeling Lucky button. Then check the page for a phrase or block of text that will tell you if it found the page you were looking for or not.

For example, you could write a function that searches for the term Columbia and then checks to see if it found the page for Columbia College Chicago by searching the page text for Chicago.

Hint: the button function can find a button by using the button's name. On the Google site the Google Search button is named btnG and the I'm Feeling Lucky button is named btnI.