ruby-on-railsrubyassertionstestunit

Continue assertion after failures in ruby


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 ?


Solution

  • 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