In Ruby's Test::Unit::TestCase, how do I override the initialize method?

前端 未结 10 1803
再見小時候
再見小時候 2020-12-07 14:54

I\'m struggling with Test::Unit. When I think of unit tests, I think of one simple test per file. But in Ruby\'s framework, I must instead write:

class M         


        
相关标签:
10条回答
  • 2020-12-07 15:27

    FINALLY, test-unit has this implemented! Woot! If you are using v 2.5.2 or later, you can just use this:

    Test::Unit.at_start do
      # initialization stuff here
    end
    

    This will run once when you start your tests off. There are also callbacks which run at the beginning of each test case (startup), in addition to the ones that run before every test (setup).

    http://test-unit.rubyforge.org/test-unit/en/Test/Unit.html#at_start-class_method

    0 讨论(0)
  • 2020-12-07 15:32

    I know this is quite an old post, but I had the issue (and had already written classes using Tes/unit) and ave answered using another method, so if it can help...

    If you only need the equivalent of the startup function, you can use the class variables:

    class MyTest < Test::Unit::TestCase
      @@cmptr = nil
      def setup
        if @@cmptr.nil?
          @@cmptr = 0
          puts "runs at first test only"
          @@var_shared_between_fcs = "value"
        end
        puts 'runs before each test'
      end
      def test_stuff
        assert(true)
      end
    end
    
    0 讨论(0)
  • 2020-12-07 15:34

    I came across this exact problem and created a subclass of Test::Unit::TestCase for doing exactly what you describe.

    Here's what I came up with. It provides it's own setup and teardown methods that count the number of methods in the class that begin with 'test'. On the first call to setup it calls global_setup and on the last call to teardown it calls global_teardown

    class ImprovedUnitTestCase < Test::Unit::TestCase
      cattr_accessor :expected_test_count
    
      def self.global_setup; end
      def self.global_teardown; end    
    
      def teardown
        if((self.class.expected_test_count-=1) == 0)
          self.class.global_teardown
        end
      end
      def setup
        cls = self.class
    
        if(not cls.expected_test_count)
          cls.expected_test_count = (cls.instance_methods.reject{|method| method[0..3] != 'test'}).length
          cls.global_setup
        end
      end
    end
    

    Create your test cases like this:

    class TestSomething < ImprovedUnitTestCase
      def self.global_setup
        puts 'global_setup is only run once at the beginning'
      end
    
      def self.global_teardown
        puts 'global_teardown is only run once at the end'
      end
    
      def test_1 
      end
    
      def test_2
      end
    end
    

    The fault in this is that you can't provide your own per-test setup and teardown methods unless you use the setup :method_name class method (only available in Rails 2.X?) and if you have a test suite or something that only runs one of the test methods, then the global_teardown won't be called because it assumes that all the test methods will be run eventually.

    0 讨论(0)
  • 2020-12-07 15:36

    To solve this problem I used the setup construct, with only one test method followed. This one testmethod is calling all other tests.

    For instance

    class TC_001 << Test::Unit::TestCase
      def setup
        # do stuff once
      end
    
      def testSuite
        falseArguments()
        arguments()
      end
    
      def falseArguments
        # do stuff
      end
    
      def arguments
        # do stuff
      end
    end
    
    0 讨论(0)
提交回复
热议问题