.

ruby test cases

That’s how it’s supposed to work!

Each test should be completely isolated from the rest, so the setup and tear_down methods are executed once for every test-case. There are cases, however, when you might want more control over the execution flow. Then you can group the test-cases in suites.

In your case you could write something like the following:

<code><span class="pln">require </span><span class="str">'test/unit'</span><span class="pln"><br />require </span><span class="str">'test/unit/ui/console/testrunner'</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">TestDecorator</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestSuite</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> initialize</span><span class="pun">(</span><span class="pln">test_case_class</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; </span><span class="kwd">super</span><span class="pln"><br />&nbsp; &nbsp; </span><span class="kwd">self</span><span class="pln"> </span><span class="pun">&lt;&lt;</span><span class="pln"> test_case_class</span><span class="pun">.</span><span class="pln">suite<br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> run</span><span class="pun">(</span><span class="pln">result</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">progress_block</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; setup_suite<br />&nbsp; &nbsp; </span><span class="kwd">begin</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; </span><span class="kwd">super</span><span class="pun">(</span><span class="pln">result</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">progress_block</span><span class="pun">)</span><span class="pln"> &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; </span><span class="kwd">ensure</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; tear_down_suite<br />&nbsp; &nbsp; </span><span class="kwd">end</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MyTestCase</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestCase</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_1<br />&nbsp; &nbsp; puts </span><span class="str">"test_1"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_2<br />&nbsp; &nbsp; puts </span><span class="str">"test_2"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MySuite</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">TestDecorator</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> setup_suite<br />&nbsp; &nbsp; puts </span><span class="str">"setup_suite"</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> tear_down_suite<br />&nbsp; &nbsp; puts </span><span class="str">"tear_down_suite"</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="pln">UI</span><span class="pun">::</span><span class="typ">Console</span><span class="pun">::</span><span class="typ">TestRunner</span><span class="pun">.</span><span class="pln">run</span><span class="pun">(</span><span class="typ">MySuite</span><span class="pun">.</span><span class="pln">new</span><span class="pun">(</span><span class="typ">MyTestCase</span><span class="pun">))</span><span class="pln"><br /></span></code>

The TestDecorator defines a special suite which provides a setup and tear_down method which run only once before and after the running of the set of test-cases it contains.

The drawback of this is that you need to tell Test::Unit how to run the tests in the unit. In the event your unit contains many test-cases and you need a decorator for only one of them you’ll need something like this:

<code><span class="pln">require </span><span class="str">'test/unit'</span><span class="pln"><br />require </span><span class="str">'test/unit/ui/console/testrunner'</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">TestDecorator</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestSuite</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> initialize</span><span class="pun">(</span><span class="pln">test_case_class</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; </span><span class="kwd">super</span><span class="pln"><br />&nbsp; &nbsp; </span><span class="kwd">self</span><span class="pln"> </span><span class="pun">&lt;&lt;</span><span class="pln"> test_case_class</span><span class="pun">.</span><span class="pln">suite<br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> run</span><span class="pun">(</span><span class="pln">result</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">progress_block</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; setup_suite<br />&nbsp; &nbsp; </span><span class="kwd">begin</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; </span><span class="kwd">super</span><span class="pun">(</span><span class="pln">result</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">progress_block</span><span class="pun">)</span><span class="pln"> &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; </span><span class="kwd">ensure</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; tear_down_suite<br />&nbsp; &nbsp; </span><span class="kwd">end</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MyTestCase</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestCase</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_1<br />&nbsp; &nbsp; puts </span><span class="str">"test_1"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_2<br />&nbsp; &nbsp; puts </span><span class="str">"test_2"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MySuite</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">TestDecorator</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> setup_suite<br />&nbsp; &nbsp; puts </span><span class="str">"setup_suite"</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> tear_down_suite<br />&nbsp; &nbsp; puts </span><span class="str">"tear_down_suite"</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AnotherTestCase</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestCase</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_a<br />&nbsp; &nbsp; puts </span><span class="str">"test_a"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="str">"a"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"a"</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Tests</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">suite<br />&nbsp; &nbsp; suite </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestSuite</span><span class="pun">.</span><span class="pln">new<br />&nbsp; &nbsp; suite </span><span class="pun">&lt;&lt;</span><span class="pln"> </span><span class="typ">MySuite</span><span class="pun">.</span><span class="pln">new</span><span class="pun">(</span><span class="typ">MyTestCase</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; suite </span><span class="pun">&lt;&lt;</span><span class="pln"> </span><span class="typ">AnotherTestCase</span><span class="pun">.</span><span class="pln">suite<br />&nbsp; &nbsp; suite<br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="pln">UI</span><span class="pun">::</span><span class="typ">Console</span><span class="pun">::</span><span class="typ">TestRunner</span><span class="pun">.</span><span class="pln">run</span><span class="pun">(</span><span class="typ">Tests</span><span class="pun">.</span><span class="pln">suite</span><span class="pun">)</span><span class="pln"><br /></span></code>

The Test::Unit documentation documentation provides a good explanation on how suites work.

What's your thoughts on this?

*

Protected by WP Anti Spam