From: Richard Date: 2006-12-04T08:55:08+09:00 Subject: Re: Why does a test fail when I predicted it's exception class? Hi Eric, Thanks for weighing in on my question. > def test_eval_no_input e = assert_raise ArgumentError do Polish.new.eval end assert_equal 'no arguments supplied', e.message end I like the additions here a lot: 1. Capturing the exception object 2. Checking not only the correct exception type is raised but also checking the programmed message passed to 'raise' > def test_eval_bad_operator 1. I take it that to conform to unit-test's expectations, the test names should begin with test_ and not test1_, test2_ etc. 2. How about me using test_1_Whatever, test_2_SomethingElse, etc,? Wouldn't that conform equally well and cause any error msgs produced by Test to be presented in lexicographical order (with fewer than 10 tests; double digits if I wanted up to 99 tests, etc.)? 3. I like to produce tests in one-line format if they'll fit on my screen reasonably so that I can write and scan them more quickly. Do see any substantive problem in my continuing to do that? > $ ruby test_polish.rb -n /eval/ I'm running Ruby_1.8.2-15 over WinXP-Pro/SP2. I tried this command and got nothing: === Command Window ==== K:\_Projects\Ruby\TestUnitTesting\ReversePolishEvaluator>ruby RevPolishEvaluator.rb -n /eval/ K:\_Projects\Ruby\TestUnitTesting\ReversePolishEvaluator> ====== end ============ I thought the problem might be that I had no errors, so that there was nothing for Test to report. So I introduced an erroneous assertion (expressed in three lines rather than my preferred one-line format) but still got nothing. Then I ran this expanded test through SciTE and got (both in one-line format as well as three-line format): ==== SciTE output ==== >ruby RevPolishEvaluatorTest.rb Loaded suite RevPolishEvaluatorTest Started .............F.... Finished in 0.031 seconds. 1) Failure: test_5_dummy(ErrorTests) [./tc_TestSet2.rb:12]: <1> expected but was <2>. 18 tests, 18 assertions, 1 failures, 0 errors >Exit code: 1 === end === Do you have any idea why the command with the "-n" switch failed on my system? My code follows. Again, thanks for your suggestions. I look forward to your comments. Best wishes, Richard === RevPolishEvaluator.rb === ### Code same as posted earlier === end === === RevPolishEvaluatorTest.rb === # Reverse-Polish Evaluation Test require 'test/unit' require 'tc_TestSet1' # <= 13 tests that produced no errors; file not shown here require 'tc_TestSet2' === end === === tc_TestSet2.rb === require '.\RevPolishEvaluator.rb' class ErrorTests < Test::Unit::TestCase # The succinct way def test_1_noarg; assert_raise(ArgumentError) {Polish.new().eval}; end def test_2_badop; assert_raise(ArgumentError) {Polish.new(%w{2 3 $ 7 -- *}).eval}; end def test_3_missingdata e = assert_raise(ArgumentError) {Polish.new(%w{2 +}).eval} assert_equal('Stack underflow', e.message) # <= Had to change the msg to confirm to my coding end def test_4_divby0; assert_raise(ZeroDivisionError) {Polish.new(%w{2 0 /}).eval}; end def test_5_dummy assert_equal(1, 2) end end === end === Eric Hodel wrote: > On Dec 2, 2006, at 20:40 , Richard wrote: > > You guys were right on! Below is one of my actual test sets, the one > > with the exception testing. It is more complicated than the toy I > > posted, so it took me a few syntax-errors to get it to work. But then > > I got my desired: 17 tests, 17 assertions, 0 failures, 0 errors > > > > require '.\RevPolishEvaluator.rb' > > > > class ErrorTests < Test::Unit::TestCase # The succinct way > > def test1_noarg; assert_raise(ArgumentError) {Polish.new().eval}; end > > def test2_badop; assert_raise(ArgumentError) {Polish.new(%w{2 3 $ > > 7 -- > > *}).eval}; end > > def test3_missingdata; assert_raise(ArgumentError) {Polish.new(%w{2 > > +}).eval}; end > > def test4_divby0; assert_raise(ZeroDivisionError) {Polish.new(%w{2 0 > > /}).eval}; end > > end > > Better: > > require 'test/unit' > require 'rev_polish_evaluator' > > class TestPolish < Test::Unit::TestCase > > def test_eval_bad_operator > # ... > end > > def test_eval_divide_by_zero > # ... > end > > def test_eval_no_input > e = assert_raise ArgumentError do Polish.new.eval end > assert_equal 'no arguments supplied', e.message > end > > def test_eval_missing_data > # ... > end > > end > > Written this way your tests: > > * follow the test/unit naming scheme (TestBlah where Blah is the > class you're testing) > * be in the same order as the output (alphabetical) > * will work with the -n flag: > > $ ruby test_polish.rb -n /eval/ > > * will work with testrb: > > $ testrb . > > -- > Eric Hodel - drbrain@segment7.net - http://blog.segment7.net > > I LIT YOUR GEM ON FIRE!