Search code examples
pythonpython-unittest

Python Unittest structure


So in my project I have a long (~1000 lines)TestCase module with only 1 TestCase.
I want to divide it into 3-4 separate TestCase modules (separate files).
But how can I make a common setUp to share fixtures for all modules?
Use some import fixtures?
I use setUpClass in that my long module with a single TestCase.
Pls, advise.


Solution

  • What you can do is define a parent class, e.g.

    class CommonTestCase(unittest.TestCase):
        def setUpClass(self):
            # Do setup or anything else here
    

    and then in other files or cases you can use inheritance to use the same setup steps from parent class CommonTestCase:

    from my_package import CommonTestCase
    
    class MyTestCaseA(CommonTestCase):
        def test_this(self):
            # Test implementation
    
    class MyTestCaseB(CommonTestCase):
        def test_that(self):
            # Test implementation