Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django test global setup

I have some file for unit test with django:

test1.py

class Test1(unittest.TestCase):
    def setUp(self):
        ...

    def tearDown(self):
        ...

test1.py

class Test1(unittest.TestCase):
    def setUp(self):
       ...

    def tearDown(self):
        ...

testn.py

class Testn(unittest.TestCase):
    def setUp(self):
       ...

    def tearDown(self):
        ...

I want to create a global setup to make some configuration for it all test, someting like:

some_file.py

class GlobalSetUpTest(SomeClass):
    def setup(self): # or any function name
         global_stuff = "whatever"

is that possible? if so, how? Thanks in advance.

like image 362
Andres Avatar asked Aug 22 '14 15:08

Andres


1 Answers

You could just create a parent class with your custom global setUp method and then have all of your other test classes extend that:

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.global_stuff = "whatever"


class TestOne(MyTestCase):
    def test_one(self):
        a = self.global_stuff 


class TestTwo(MyTestCase):
    def setUp(self):
        # Other setUp operations here
        super(TestTwo, self).setUp() # this will call MyTestCase.setUp to ensure self.global_stuff is assigned.

    def test_two(self):
        a = self.global_stuff

Obviously you could use the same technique for a 'global' tearDown method.

like image 163
dgel Avatar answered Oct 17 '22 21:10

dgel