Difference between revisions of "Unit Testing"
Arif Iqbal (talk | contribs) (Document started) |
Arif Iqbal (talk | contribs) |
||
| Line 21: | Line 21: | ||
assert_equal(9, @myclass.square(3)) | assert_equal(9, @myclass.square(3)) | ||
end | end | ||
| − | def | + | def test_double |
assert_equal(6, @myclass.square(3)) | assert_equal(6, @myclass.square(3)) | ||
end | end | ||
end | end | ||
| − | setup is used to initialize any variables for the tests. All the test cases start with the test keyword like test_square etc. when we run this test we get | + | setup is used to initialize any variables for the tests. All the test cases start with the test keyword like test_square etc. assert_equal input parameters are expected output and the actual output i.e. assert_equal(expected_output, actual_output). when we run this test we get |
Loaded suite testing_example | Loaded suite testing_example | ||
| Line 33: | Line 33: | ||
Finished in 0.007477 seconds. | Finished in 0.007477 seconds. | ||
1) Failure: | 1) Failure: | ||
| − | + | test_double(TestMyClass) [testing_example.rb:25]: | |
<6> expected but was | <6> expected but was | ||
| − | <9>. | + | <9>. |
2 tests, 2 assertions, 1 failures, 0 errors | 2 tests, 2 assertions, 1 failures, 0 errors | ||
| + | |||
| + | As the last line in the output shows that one test(test_double) failed out of the two tests. | ||
Revision as of 05:42, 15 August 2007
Unit Testing means testing small chunks of code. We want to be sure that when we write any software we are really DoneDone. TestDrivenDevelopment says that you should write your tests before writing any code as the tests gives you insights into what exactly you want to do. Then incrementally you keep on passing the test cases and when you have passed all the test cases, you are then confident that the code works
Following is a small example of unit testing in Ruby. We have made a new class named MyClass and have written a small unit test named TestMyClass to test this class
require 'test/unit'
class MyClass
def square(val)
return val*val
end
def double(val)
return val
end
end
class TestMyClass expected but was
.
2 tests, 2 assertions, 1 failures, 0 errors
As the last line in the output shows that one test(test_double) failed out of the two tests.
