Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Instead of using <code>unittest.main()</code>, which automatically goes through all your test classes and individual tests, you can load the tests from each class separately and execute them at your discretion, including before each class test the necessary initializations. You can precede all that with some global initialization as well. See an example below:</p> <pre><code>import unittest class MyTestClass1(unittest.TestCase): def setUp(self): pass @classmethod def setUpClass(cls): pass def test1(self): pass # lots of tests class MyTestClass2(unittest.TestCase): def setUp(self): pass @classmethod def setUpClass(cls): pass def test1(self): pass # another whole lot of tests if __name__=="__main__": # add your global initialization code here global_initialization() # MyTestClass1 initialization: MyTestClass1.setUpClass() # python 2.7 only suite = unittest.TestLoader().loadTestsFromTestCase(MyTestClass1) unittest.TextTestRunner().run(suite) # MyTestClass1 initialization: MyTestClass2.setUpClass() # python 2.7 only suite = unittest.TestLoader().loadTestsFromTestCase(MyTestClass2) unittest.TextTestRunner().run(suite) </code></pre> <p>The <code>unittest</code> <a href="http://docs.python.org/library/unittest.html" rel="nofollow">documentation</a> has some other examples of similar use which could be of assistance.</p> <p><strong>Edit:</strong> Unbeknown to me until this moment, it seems that Python 2.7 has a <code>setUpClass()</code>, (should be a class method, hence the decorator) which seems to do what you need it to . However, that's not the case in Python 2.6 and you should provide your own initialization routines.</p>
 

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