Sunday, February 8, 2009

Topic for the next session: Test::Unit

Hi,

I am planning to go over Test::Unit in the next session.

Test::Unit is the basic testing framework that is built into Ruby. There are other frameworks for testing that are probably better (RSpec, Shoulda), but I figured that Test::Unit would be a good place to start.

There is a program that I found in a excellent introductory book called the Ruby Visual QuickStart Guide written by a guy named Larry Ullman. The program is called testing.rb, and it's in a zip file at the following link:

http://www.dmcinsights.com/ruby/downloads.php.

The link for the zip file is towards the bottom of the screen and a bit to the left. Once you unzip the file, you'll file the testing.rb file in the scripts/11 folder.

I recommend taking a look at the program before the session this week. Basically, it's a program with a Rectangle class with an initialize method that defines a rectangle. Pretty basic stuff that the regulars to the Ruby sessions should be familiar with. There is also another class in there which will be new to most of you. It's the TestRectangle class, and it is a subclass of Test::Unit::TestCase. There are various assertions in TestRectangle. Assertions are what the tests are called. If you execute the program, the tests in TestRectangle are executed and the results are written to the console. There are 3 tests, 11 assertions, 0 failures and 0 errors in this example.

To understand what is going on in TestRectangle, I recommend messing with the code a bit. If you change the code on line 35 to:

assert(Rectangle.new(6, 6.1).is_square?)

you will get an failure as expected, because the inputs to Rectangle are not equal, and therefore the shape created is not a square.

You can even mess with the code in the Rectangle class. For example, if you change line 15 to:

(@height + @width) * 2.1

you will get a failure because the perimeter method is no longer correct.


See you (via the Internet) at the next session.

Thanks,

Glenn

No comments: