Creating and Using Fixtures

What’s a fixture?

A fixture is a way of creating a fixed environment where we can
run tests in a repeatable fashion. When we talk about fixtures for Rasta,
we’re referring to the various ways we can read test data from a spreadsheet
and generate a set of RSpec tests from that data. We can plug in multiple
fixtures into this framework to give us an expanding set of paradigms
to use in our testing. For example, the first fixture we’ve implemented
is the Column Fixture which is basically setting attributes and performing
method calls and comparing the method’s result to the value in the spreadsheet.
This is very similar to FIT so if you’ve used that product, much of this
will be familiar.

So in this manner, Rasta provides a set of fixtures for you to use. Since
the test framework knows nothing of the software under test, it’s up to
you to create a test fixture based on one of the Rasta fixtures that interacts
with your application. Your test fixture could be checking database values,
using Watir to run GUI automation, etc. There are several examples provided
that should help give you a better idea of how to proceeed.

All fixtures are created from a base fixture that interacts with RSpec
and reports results back to the spreadsheet by changing the background
color of the cells; red for failure and green for success. Further, any
errors encountered are detailed in the cell’s comment field (Excel puts
a red triangle in the cell’s corner to indicate a comment exists).

When you run the ‘rasta’ executable on a commandline, you’ll give it a path
to your testfixtures and the spreadsheet you want to run. As the test progresses
you can see the spreadsheet being updated and the standard RSpec commandline
output. Results are stored in a directory called ‘rasta_test_results’
that will be automatically created in the same directory where rasta is
invoked (this can be overridden with a commandline option). This directory
contains a copy of the commandline output, the same output formatted in html
and a copy of the spreadsheet results.

Rasta Fixture

The first fixture we’ve implemented is the rasta fixture, which is like a
combination of FIT’s row and column fixtures. The spreadsheet is parsed where
the spreadsheet columns are either class attributes or methods. These attributes
and methods are defined in your test fixture and are called row by row, left
to right (or column by column, top to bottom).


Column Configuration


Row Configuration

When the fixture gets to a method, the fixture creates a few rspec tests to
cover the following possible states:

The spreadsheet cell’s expected results will then be compared to the actual
results. The list below shows the different data types supported in
the cells.

Arrays

You can pass in an array of values using []. So [1,2,3] and ‘a’,‘b’,‘c’
will be interpreted as arrays. You can also split these (and hashes) into
multiple lines to make things easier to read in Excel, using ALT+ENTER.

Hashes

You can pass in a hash of values using {}. So {’a’=>1, ‘b’=>2} will be
treated as a hash

Numbers

When a cell contains only digits (and an option decimal), the value will
be treated as an actual number, not a string.

Nil

For methods, the Rasta fixture will make the conversion to the Ruby nil.
That allows you to put nil in spreadsheet cells to run and match methods that
have no well-defined return values. While there’s not much to match on,
the fixture will still catch exceptions. If you would like to handle nil
with attributes(say you want to use it as a way to remove the value from a
text field when using watir) then your test fixture will need to handle
that functionality.

Exceptions

If you expect to get an exception you can put the string ‘error’ into the
cell. If the method throws an exception then this cell will be marked as
a success. Further, you can catch a specific error using ‘error ExceptionName’
or get more specific and match on the message with ‘error ExceptionName: message’

For example in the sample rasta_fixture.xls you could match the exception
in MathFunctions with ‘error’, ‘error ZeroDivisionError’ or
‘error ZeroDivisionError: divided by 0’

Pattern Matches

Cell values bounded by // will be treated as a regular expression. This should
be very powerful and allow you to effectively create custom ways to define a
successful test result.

Comparison Matches

Cells can also have a fuzzier match for numerical values with >, >=, <, <=.

Creating a Test Fixture using the Rasta Fixture

To use the Rasta Fixture, you need to require the fixture and then include
it in your class. This will instrument the class with the methods it needs
to be called from the spreadsheet. This is an example of the bare minimum
you would need. Your spreadsheet would have headers for username, pasword,
and login.

require 'rasta/fixture/rasta_fixture'

class ApplicationLogin
   include Rasta::Fixture::RastaFixture
   attr_accessor :username, :password 
   
   def login
      # some code to login to an application   
   end
end

This will get us started but it’s not entirely practical. So lets
make this run under Watir and also take advantage of some of
the setup and teardown functionality:

require 'watir'
require 'rasta/fixture/rasta_fixture'

class ApplicationLogin
   include Rasta::Fixture::RastaFixture
   attr_accessor :username, :password 
   
   def before_all
     @@ie = Watir::IE.new
     @@ie.set_fast_speed
   end
   
   def before_each
     @@ie.goto("http://www.my-login-page.com")
   end
   
   def login
     @@ie.text_field(:name, 'username').set(@username)
     @@ie.text_field(:name, 'password').set(@password)
     return false unless @@ie.contains_text('Login Succesful')
   end
 
   def after_all
    @@ie.close
   end
   
end

You also have access to a few class variables that you may
find useful for some of your customizations:

These are all available in your test fixture when you include the Rasta
fixture so you can use these anywhere in the class.