Unit Testing Ruby on Netbeans IDE
For ruby unit testing, we need to extend from the class "Test::Unit::TestCase?".
Suppose the following "Demo" class exists. It has only one method ie “return_two”.
class Demo
def return_two
2
end
end
The method "return_two" returns the value 2. We could have written "return 2" or just "2" since ruby returns the value of the last line in the method.
Now let us write the class to test the "Demo" class. I have used Netbeans IDE.
By convention, we have to name test classes as "Test+ClassName?". So we have "TestDemo?" as the classname. Here is the ruby code.
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
require 'test/unit'
require 'demo'
class TestDemo < Test::Unit::TestCase
def test_foo
d= Demo.new
assert_equal 2, d. return_two
# assert_equal("foo", bar)
end
def test_fail
d= Demo.new
assert_equal 3, d. return_two
end
end
Now the method "test_foo" tests the method "return_two" of "Demo" class.
In detail:
1. First the class “Demo” is instantiated using “Demo.new”
2. Next, the variable “d” is used to store the reference to the newly instantiated “Demo” object.
3. The method “assert_equal” checks whether the expected value (2) matches with the actual value (d. return_two). Since this matches, the test case will pass.
4. There is another method called “test_fail”. This checks whether the expected value (3) is equal to the actual value (d. return_two). Of course, this method will fail.
When we run the “TestDemo?” program, following output is obtained.
Loaded suite C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\test_demo
Started
F.
Finished in 0.131 seconds.
1) Failure:
test_fail(TestDemo) [C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\test_demo.rb:14]:
<3> expected but was
<2>.
2 tests, 2 assertions, 1 failures, 0 errors
Running multiple tests – Testsuite ¶
Suppose we have another class “example”.
Here is the code.
class Example
def return_five
5
end
end
The method “return_five” returns 5.
So here is the class “TestExample?” to test the above class.
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
require 'test/unit'
require 'example'
class TestExample < Test::Unit::TestCase
def test_foo
k=Example.new
assert_equal 5, k.return_five,"Success"
end
def test_fail
k=Example.new
assert_equal 6, k.return_five,"Success"
end
end
The sample method “test_foo” would always pass.
The method “test_fail” would always fail.
Here is the output when the “TestExample?” class is run.
Loaded suite C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\test_example
Started
F.
Finished in 0.195 seconds.
1) Failure:
test_fail(TestExample) [C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\test_example.rb:21]:
Success.
<6> expected but was
<5>.
2 tests, 2 assertions, 1 failures, 0 errors
Now we have two classes
1. TestExample?
2. TestDemo?
We need to run the tests in both the classes. Test suite comes into the picture.
TestSuite? code ¶
Create a test suite class, say “ts_suite”
require 'test/unit'
# Add your testcases here
require 'test_example'
require 'test_demo'
#require 'tc_3'
The above test suite says
1. First run the test case "test_example".
2. Then run the test case "test_demo".
3. It will not run "tc_3" since it is commented out.
We can go on adding test cases to the test suite as above. Here is the output from the test suite when it is run.
Loaded suite C:\Documents and Settings\manoj_tharayil\My Documents\NetBeansProjects\RubyTesting\lib\ts_suite
Started
F.F.
Finished in 0.17500000000000002 seconds.
1) Failure:
test_fail(TestDemo) [C:/Documents and Settings/manoj_tharayil/My Documents/NetBeansProjects/RubyTesting/lib/../lib/test_demo.rb:14]:
<3> expected but was
<2>.
2) Failure:
test_fail(TestExample) [C:/Documents and Settings/manoj_tharayil/My Documents/NetBeansProjects/RubyTesting/lib/test_example.rb:21]:
Success.
<6> expected but was
<5>.
4 tests, 4 assertions, 2 failures, 0 errors
You can see that there are 2 failures – one in each class.
The failures are “hyperlinks” – when clicked would take us to the exact line in the method of the class which failed.
Happy Testing!!
Comments
Post a Comment