3

I want to create unittest test with two different set up and tearDown methon in same class with two different test.

each test will use its specific setUp and tearDown method in python unittest framework.

could anyone help me.

    class processtestCase(unittest.TestCase):

         print "start the process test cases " 

        def setUp1(self): 
             unittest.TestCase.setUp(self)

        def test_test1(self): 
              "test Functinality" 

        def tearDown1(self): 
             unittest.TestCase.tearDown(self) 

        def setUp2(self): 
            unittest.TestCase.setUp2(self) 

        def test_test2(self): 
            "test Functinality" 

        def tearDown2(self): 
            unittest.TestCase.tearDown2(self) '

if __name__ == '__main__':
        unittest.main()
  • Could you provide an example of what you're trying to achieve? If the tests need separate set up and tear down, why are they in the same class? – jonrsharpe Jul 15 '16 at 11:36
  • We need to create set of test specific to functionality so we don,t need to create separate class for every test but some of those test require some different set up so that why I need to add different setUp and tearDown for those test. Like In TestNg in java we can give multiple testSetup in same class so is it possible in python unittest ? – Gajendra Chavan Jul 15 '16 at 12:32
  • that doesn't answer either of my questions. Give some examples. – jonrsharpe Jul 15 '16 at 12:33
  • example 'code' class processtestCase(unittest.TestCase): print "start the process test cases for ingestionAPI" def setUp1(self): unittest.TestCase.setUp(self) def test_test1(self): "test Functinality" def tearDown1(self): unittest.TestCase.tearDown(self) def setUp2(self): unittest.TestCase.setUp2(self) def test_test2(self): "test Functinality" def tearDown2(self): unittest.TestCase.tearDown2(self) 'code' – Gajendra Chavan Jul 15 '16 at 12:38
  • Oh, for pity's... **[edit] the question!!** – jonrsharpe Jul 15 '16 at 12:39

1 Answers1

4

In the question, you mention that you have two tests, each with its own setup and teardown. There are at least two ways to go:

You can either embed the setUp and tearDown code into each of the tests:

class FooTest(unittest.TestCase):
    def test_0(self):
        ... # 1st setUp() code
        try:
            ... # 1st test code
        except:
            ... # 1st tearDown() code
            raise

    def test_1(self):
        ... # 2nd setUp() code
        try:
            ... # 2nd test code
        except:
            ... # 2nd tearDown() code
            raise

Alternatively, you can split the class into two classes:

class FooTest0(unittest.TestCase):
    @classmethod
    def setUp(cls):
        ...

    @classmethod
    def tearDown(cls):
        ...

    def test(self):
       ...

The first option has fewer classes, is shorter, and more straightforsard. The second option more cleanly decouples setting up the fixture, and cleaning it up, then the test code itself. It also future proofs adding more tests.

You should judge the tradeoffs based on your specific case, and your personal preferences.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185