Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came across this exact problem and created a subclass of <code>Test::Unit::TestCase</code> for doing exactly what you describe.</p> <p>Here's what I came up with. It provides it's own <code>setup</code> and <code>teardown</code> methods that count the number of methods in the class that begin with 'test'. On the first call to <code>setup</code> it calls <code>global_setup</code> and on the last call to <code>teardown</code> it calls <code>global_teardown</code></p> <pre><code>class ImprovedUnitTestCase &lt; 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 </code></pre> <p>Create your test cases like this:</p> <pre><code>class TestSomething &lt; 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 </code></pre> <p>The fault in this is that you can't provide your own per-test <code>setup</code> and <code>teardown</code> methods unless you use the <code>setup :method_name</code> 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 <code>global_teardown</code> won't be called because it assumes that all the test methods will be run eventually.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload