<s> import sys <EOL> from cStringIO import StringIO <EOL> import unittest2 <EOL> from unittest2 . test . support import resultFactory <EOL> class TestSetups ( unittest2 . TestCase ) : <EOL> def getRunner ( self ) : <EOL> return unittest2 . TextTestRunner ( resultclass = resultFactory , <EOL> stream = StringIO ( ) ) <EOL> def runTests ( self , * cases ) : <EOL> suite = unittest2 . TestSuite ( ) <EOL> for case in cases : <EOL> tests = unittest2 . defaultTestLoader . loadTestsFromTestCase ( case ) <EOL> suite . addTests ( tests ) <EOL> runner = self . getRunner ( ) <EOL> realSuite = unittest2 . TestSuite ( ) <EOL> realSuite . addTest ( suite ) <EOL> suite . addTest ( unittest2 . TestSuite ( ) ) <EOL> realSuite . addTest ( unittest2 . TestSuite ( ) ) <EOL> return runner . run ( realSuite ) <EOL> def test_setup_class ( self ) : <EOL> class Test ( unittest2 . TestCase ) : <EOL> setUpCalled = <NUM_LIT:0> <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> Test . setUpCalled += <NUM_LIT:1> <EOL> unittest2 . TestCase . setUpClass ( ) <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> result = self . runTests ( Test ) <EOL> self . assertEqual ( Test . setUpCalled , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:0> ) <EOL> def test_teardown_class ( self ) : <EOL> class Test ( unittest2 . TestCase ) : <EOL> tearDownCalled = <NUM_LIT:0> <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Test . tearDownCalled += <NUM_LIT:1> <EOL> unittest2 . TestCase . tearDownClass ( ) <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> result = self . runTests ( Test ) <EOL> self . assertEqual ( Test . tearDownCalled , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:0> ) <EOL> def test_teardown_class_two_classes ( self ) : <EOL> class Test ( unittest2 . TestCase ) : <EOL> tearDownCalled = <NUM_LIT:0> <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Test . tearDownCalled += <NUM_LIT:1> <EOL> unittest2 . TestCase . tearDownClass ( ) <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> class Test2 ( unittest2 . TestCase ) : <EOL> tearDownCalled = <NUM_LIT:0> <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Test2 . tearDownCalled += <NUM_LIT:1> <EOL> unittest2 . TestCase . tearDownClass ( ) <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> result = self . runTests ( Test , Test2 ) <EOL> self . assertEqual ( Test . tearDownCalled , <NUM_LIT:1> ) <EOL> self . assertEqual ( Test2 . tearDownCalled , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:4> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:0> ) <EOL> def test_error_in_setupclass ( self ) : <EOL> class BrokenTest ( unittest2 . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> raise TypeError ( '<STR_LIT:foo>' ) <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> result = self . runTests ( BrokenTest ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:1> ) <EOL> error , _ = result . errors [ <NUM_LIT:0> ] <EOL> self . assertEqual ( str ( error ) , <EOL> '<STR_LIT>' % __name__ ) <EOL> def test_error_in_teardown_class ( self ) : <EOL> class Test ( unittest2 . TestCase ) : <EOL> tornDown = <NUM_LIT:0> <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Test . tornDown += <NUM_LIT:1> <EOL> raise TypeError ( '<STR_LIT:foo>' ) <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> class Test2 ( unittest2 . TestCase ) : <EOL> tornDown = <NUM_LIT:0> <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Test2 . tornDown += <NUM_LIT:1> <EOL> raise TypeError ( '<STR_LIT:foo>' ) <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> result = self . runTests ( Test , Test2 ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:4> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( Test . tornDown , <NUM_LIT:1> ) <EOL> self . assertEqual ( Test2 . tornDown , <NUM_LIT:1> ) <EOL> error , _ = result . errors [ <NUM_LIT:0> ] <EOL> self . assertEqual ( str ( error ) , <EOL> '<STR_LIT>' % __name__ ) <EOL> def test_class_not_torndown_when_setup_fails ( self ) : <EOL> class Test ( unittest2 . TestCase ) : <EOL> tornDown = False <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> raise TypeError <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Test . tornDown = True <EOL> raise TypeError ( '<STR_LIT:foo>' ) <EOL> def test_one ( self ) : <EOL> pass <EOL> self . runTests ( Test ) <EOL> self . assertFalse ( Test . tornDown ) <EOL> def test_class_not_setup_or_torndown_when_skipped ( self ) : <EOL> class Test ( unittest2 . TestCase ) : <EOL> classSetUp = False <EOL> tornDown = False <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> Test . classSetUp = True <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Test . tornDown = True <EOL> def test_one ( self ) : <EOL> pass <EOL> Test = unittest2 . skip ( "<STR_LIT>" ) ( Test ) <EOL> self . runTests ( Test ) <EOL> self . assertFalse ( Test . classSetUp ) <EOL> self . assertFalse ( Test . tornDown ) <EOL> def test_setup_teardown_order_with_pathological_suite ( self ) : <EOL> results = [ ] <EOL> class Module1 ( object ) : <EOL> @ staticmethod <EOL> def setUpModule ( ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def tearDownModule ( ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> class Module2 ( object ) : <EOL> @ staticmethod <EOL> def setUpModule ( ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def tearDownModule ( ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> class Test1 ( unittest2 . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> def testOne ( self ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> def testTwo ( self ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> class Test2 ( unittest2 . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> def testOne ( self ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> def testTwo ( self ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> class Test3 ( unittest2 . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> def testOne ( self ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> def testTwo ( self ) : <EOL> results . append ( '<STR_LIT>' ) <EOL> Test1 . __module__ = Test2 . __module__ = '<STR_LIT>' <EOL> Test3 . __module__ = '<STR_LIT>' <EOL> sys . modules [ '<STR_LIT>' ] = Module1 <EOL> sys . modules [ '<STR_LIT>' ] = Module2 <EOL> first = unittest2 . TestSuite ( ( Test1 ( '<STR_LIT>' ) , ) ) <EOL> second = unittest2 . TestSuite ( ( Test1 ( '<STR_LIT>' ) , ) ) <EOL> third = unittest2 . TestSuite ( ( Test2 ( '<STR_LIT>' ) , ) ) <EOL> fourth = unittest2 . TestSuite ( ( Test2 ( '<STR_LIT>' ) , ) ) <EOL> fifth = unittest2 . TestSuite ( ( Test3 ( '<STR_LIT>' ) , ) ) <EOL> sixth = unittest2 . TestSuite ( ( Test3 ( '<STR_LIT>' ) , ) ) <EOL> suite = unittest2 . TestSuite ( ( first , second , third , fourth , fifth , sixth ) ) <EOL> runner = self . getRunner ( ) <EOL> result = runner . run ( suite ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:6> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( results , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_setup_module ( self ) : <EOL> class Module ( object ) : <EOL> moduleSetup = <NUM_LIT:0> <EOL> @ staticmethod <EOL> def setUpModule ( ) : <EOL> Module . moduleSetup += <NUM_LIT:1> <EOL> class Test ( unittest2 . TestCase ) : <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> Test . __module__ = '<STR_LIT>' <EOL> sys . modules [ '<STR_LIT>' ] = Module <EOL> result = self . runTests ( Test ) <EOL> self . assertEqual ( Module . moduleSetup , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:0> ) <EOL> def test_error_in_setup_module ( self ) : <EOL> class Module ( object ) : <EOL> moduleSetup = <NUM_LIT:0> <EOL> moduleTornDown = <NUM_LIT:0> <EOL> @ staticmethod <EOL> def setUpModule ( ) : <EOL> Module . moduleSetup += <NUM_LIT:1> <EOL> raise TypeError ( '<STR_LIT:foo>' ) <EOL> @ staticmethod <EOL> def tearDownModule ( ) : <EOL> Module . moduleTornDown += <NUM_LIT:1> <EOL> class Test ( unittest2 . TestCase ) : <EOL> classSetUp = False <EOL> classTornDown = False <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> Test . classSetUp = True <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Test . classTornDown = True <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> class Test2 ( unittest2 . TestCase ) : <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> Test . __module__ = '<STR_LIT>' <EOL> Test2 . __module__ = '<STR_LIT>' <EOL> sys . modules [ '<STR_LIT>' ] = Module <EOL> result = self . runTests ( Test , Test2 ) <EOL> self . assertEqual ( Module . moduleSetup , <NUM_LIT:1> ) <EOL> self . assertEqual ( Module . moduleTornDown , <NUM_LIT:0> ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:0> ) <EOL> self . assertFalse ( Test . classSetUp ) <EOL> self . assertFalse ( Test . classTornDown ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:1> ) <EOL> error , _ = result . errors [ <NUM_LIT:0> ] <EOL> self . assertEqual ( str ( error ) , '<STR_LIT>' ) <EOL> def test_testcase_with_missing_module ( self ) : <EOL> class Test ( unittest2 . TestCase ) : <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> Test . __module__ = '<STR_LIT>' <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> result = self . runTests ( Test ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:2> ) <EOL> def test_teardown_module ( self ) : <EOL> class Module ( object ) : <EOL> moduleTornDown = <NUM_LIT:0> <EOL> @ staticmethod <EOL> def tearDownModule ( ) : <EOL> Module . moduleTornDown += <NUM_LIT:1> <EOL> class Test ( unittest2 . TestCase ) : <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> Test . __module__ = '<STR_LIT>' <EOL> sys . modules [ '<STR_LIT>' ] = Module <EOL> result = self . runTests ( Test ) <EOL> self . assertEqual ( Module . moduleTornDown , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:0> ) <EOL> def test_error_in_teardown_module ( self ) : <EOL> class Module ( object ) : <EOL> moduleTornDown = <NUM_LIT:0> <EOL> @ staticmethod <EOL> def tearDownModule ( ) : <EOL> Module . moduleTornDown += <NUM_LIT:1> <EOL> raise TypeError ( '<STR_LIT:foo>' ) <EOL> class Test ( unittest2 . TestCase ) : <EOL> classSetUp = False <EOL> classTornDown = False <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> Test . classSetUp = True <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Test . classTornDown = True <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> class Test2 ( unittest2 . TestCase ) : <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> Test . __module__ = '<STR_LIT>' <EOL> Test2 . __module__ = '<STR_LIT>' <EOL> sys . modules [ '<STR_LIT>' ] = Module <EOL> result = self . runTests ( Test , Test2 ) <EOL> self . assertEqual ( Module . moduleTornDown , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:4> ) <EOL> self . assertTrue ( Test . classSetUp ) <EOL> self . assertTrue ( Test . classTornDown ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:1> ) <EOL> error , _ = result . errors [ <NUM_LIT:0> ] <EOL> self . assertEqual ( str ( error ) , '<STR_LIT>' ) <EOL> def test_skiptest_in_setupclass ( self ) : <EOL> class Test ( unittest2 . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> raise unittest2 . SkipTest ( '<STR_LIT:foo>' ) <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> result = self . runTests ( Test ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( result . skipped ) , <NUM_LIT:1> ) <EOL> skipped = result . skipped [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( str ( skipped ) , '<STR_LIT>' % __name__ ) <EOL> def test_skiptest_in_setupmodule ( self ) : <EOL> class Test ( unittest2 . TestCase ) : <EOL> def test_one ( self ) : <EOL> pass <EOL> def test_two ( self ) : <EOL> pass <EOL> class Module ( object ) : <EOL> @ staticmethod <EOL> def setUpModule ( ) : <EOL> raise unittest2 . SkipTest ( '<STR_LIT:foo>' ) <EOL> Test . __module__ = '<STR_LIT>' <EOL> sys . modules [ '<STR_LIT>' ] = Module <EOL> result = self . runTests ( Test ) <EOL> self . assertEqual ( result . testsRun , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( result . errors ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( result . skipped ) , <NUM_LIT:1> ) <EOL> skipped = result . skipped [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( str ( skipped ) , '<STR_LIT>' ) <EOL> def test_suite_debug_executes_setups_and_teardowns ( self ) : <EOL> ordering = [ ] <EOL> class Module ( object ) : <EOL> @ staticmethod <EOL> def setUpModule ( ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def tearDownModule ( ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> class Test ( unittest2 . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> def test_something ( self ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> Test . __module__ = '<STR_LIT>' <EOL> sys . modules [ '<STR_LIT>' ] = Module <EOL> suite = unittest2 . defaultTestLoader . loadTestsFromTestCase ( Test ) <EOL> suite . debug ( ) <EOL> expectedOrder = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEqual ( ordering , expectedOrder ) <EOL> def test_suite_debug_propagates_exceptions ( self ) : <EOL> class Module ( object ) : <EOL> @ staticmethod <EOL> def setUpModule ( ) : <EOL> if phase == <NUM_LIT:0> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def tearDownModule ( ) : <EOL> if phase == <NUM_LIT:1> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> class Test ( unittest2 . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> if phase == <NUM_LIT:2> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> if phase == <NUM_LIT:3> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def test_something ( self ) : <EOL> if phase == <NUM_LIT:4> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> Test . __module__ = '<STR_LIT>' <EOL> sys . modules [ '<STR_LIT>' ] = Module <EOL> _suite = unittest2 . defaultTestLoader . loadTestsFromTestCase ( Test ) <EOL> suite = unittest2 . TestSuite ( ) <EOL> suite . addTest ( _suite ) <EOL> messages = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for phase , msg in enumerate ( messages ) : <EOL> self . assertRaisesRegexp ( Exception , msg , suite . debug ) </s>
