In this blog I’m going to show you how to apply asserts to your scripts using ruby’s built in ‘test/unit’ library. Some people are not quite sure what an assert is in programming terms but it is very simply asserting an outcome. So with this example were going to work with I am making an assertion on the expected result returned by a class. The class in our example will simply return the result of two numbers added together. So let’s look at our class that adds the numbers together first:
**Start of code
01 class Plus2
02 def initialize(a, b)
03 @ = a
04 @b = b
05 end
06
07 def add_up
08 begin
09 result = @a + @b
10 if result = = 4
11 return result
12 else
13 return 0
14 end
15 rescue
16 return 0
17 end
18 end
**End of code
We’ll save the file as ‘scriptplus2.rb’
This script simply accepts two integer values adds them together and returns the result.Line 02 to 05 uses the standard initialize method to receive our two integer values.Line 09 adds our values together and stores the new value into our variable ‘result’.Line 10 test the value of result to see if we have the value of 4, if we do we return this value if not we return a value of zero.
So lets look at our calling class:
**Start of code
01 Require “scriptplus2”
02 Require “test/unit”
03
04 class AssertTest < Test::Unit::Case
05
06 Def test_a
07 Myobject = Plus2.new(2,2)
08 assert_same(4, myobject.add_up())
09 end
10 end
**End of code
We’ll save this file as assert.rb
Line 01 requires our first file.
Line 02 requires ruby’s ‘test/unit’ library allowing our asserts to work
Line 04 defines our class ‘AssetTest’ but also attaches to our class the ‘test/unit’ methods with the ‘< Test::Unit::Case’ statement.
Line 06 defines our method. Two things to note here
Note 1: ‘test_’ ('test' followed by an underscore) must be at the start of any method name you want included in assertion testing. Any methods that do not start with the ‘test_’ identifier will not be included in assertions. This can be useful if you have methods that you do not wish to be included in assertion tests but are required by your class, setup methods etc.
Note 2: Your methods will be executed in alphabetical order. So if you have three methods defined as 'test_a', 'test_b' & 'test_c' no matter what order you place them in your class, 'test_a' will run first followed by 'test_b' & finally 'test_c'.
Line 07 creates an object of our class ‘Plus2’ from our ‘scriptplus2.rb’ file and passes to the ‘initialize’ method our two integer values.
Line 08 essentially calls our ‘add_up’ method but wrapped around our call is the asset_same method. Let’s look at this line in detail to see exactly what’s going on. What were saying here is I assert that the reply I get back from the ‘add_up’ method should be the value of 4. If 4 is returned the program ends and will report the following:
1 test, 1 assertion, 0 failures, 0 errors
If we change line 07 to ‘Myobject = Plus2.new(2,1)’ what will happen is our method will return zero because 2 + 1 = 3 and the assert report will now read:
1 test, 1 assertion, 1 failure, 0 errors
This gives a very simple example of one type of assertion but I hope it will give you a starting point from which you can develop further assertion tests.
Friday, 20 June 2008
Subscribe to:
Post Comments (Atom)


1 comments:
Well done got some nice information, keep going...
Web Development
Post a Comment