0

My assertion example is below,

class test < Test::Unit::TestCase
     def test_users
      begin
       assert_equal(user.name, 'John')
       assert_equal(user.age, 30)
       assert_equal(user.zipcode, 500002)  
      rescue Exception
       raise
      end 

     end   
   end

If any one of assertions fails, i should move on to process the next one and collect the failure assertions and show failures the end of the result.

I have used add_failure method, its working for looping condition

rescue Test::Unit::AssertionFailedError => e
          add_failure(e.message, e.backtrace)

Can any one help ?

Galet
  • 5,853
  • 21
  • 82
  • 148

3 Answers3

0

Your main problem is that assert_equal ends up calling assert (as shown below) and assert will raise an ArgumentException.

File test/unit/assertions.rb, line 144

def assert_equal(exp, act, msg = nil)
  msg = message(msg) {
  # omitted code to save space
 } 
  assert(exp == act, msg)
end

File test/unit/assertions.rb, line 29

def assert(test, msg = UNASSIGNED)
  case msg
  when UNASSIGNED
    msg = nil
  when String, Proc
  else
    bt = caller.reject { |s| s.rindex(MINI_DIR, 0) }
    raise ArgumentError, "assertion message must be String or Proc, but #{msg.class} was given.", bt
  end
  super
end

You could extend Test::Unit::Assertions and provide an assertion that does not raise ArgumentError, which is what is stopping continuation past the failed assertion.

See this question for advice on going that direction and adding in safe assertions.

Community
  • 1
  • 1
Chris
  • 788
  • 4
  • 11
0

A good unit test should test exactly one thing, specifically to avoid problems like you just face. This test case will report on all failed tests, and not just the first failed test:

class MyTest < Test::Unit::TestCase
  def test_user_name
    assert_equal(user.name, 'John')
  end

  def test_user_age 
    assert_equal(user.age, 30)
  end

  def test_user_zipcode
    assert_equal(user.zipcode, 500002)  
  end 
end   
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • 2
    I and others write one test per method call, not per assertion: http://stackoverflow.com/questions/762512/is-it-bad-practice-to-have-more-than-assert-in-a-unit-test So karan's question seems reasonable, although I haven't wanted to do what he's asking myself. – Dave Schweisguth May 08 '14 at 17:01
  • @DaveSchweisguth - I don't have a problem with putting a few assertions in one test case, but it does seem weird to patch and break the library in order to get the result you would get by simply _following its guidelines_. Make multiple asserts in one test when you know that if one fails - you don't care about the rest. – Uri Agassi May 08 '14 at 17:58
  • @UriAgassi Is it possible to show assertion failures at end when i have multiple assertions in one method. – Galet May 09 '14 at 05:07
  • The whole point of assertion is that they fail the _whole_ test. A test _suite_ shows you all the _test case_ failures, which is what you want. You can of course patch the code to enable you that, but it is like breaking a perfectly good toy... – Uri Agassi May 09 '14 at 05:16
0

Please find this code for Continue assertion after failures in Ruby :

def raise_and_rescue  
  begin  
    puts 'I am before the raise.'  
    raise 'An error has occured.'  
    puts 'I am after the raise.'  
  rescue  
    puts 'I am rescued.'  
  end  
  puts 'I am after the begin block.'  
end  

Output :

ruby p045handexcp.rb
I am before the raise.
I am rescued.
I am after the begin block.
Exit code: 0

David Buck
  • 3,752
  • 35
  • 31
  • 35