M
M
mkone1122022-01-13 22:12:58
Django
mkone112, 2022-01-13 22:12:58

How to avoid calling setUp for each test separately?

I have a suite of thousands of tests that looks something like this:

class OneOfTheBaseTest:
    def setUp(self):
        # prepare db
        self.create_test_data_method_1()
        self.create_test_data_method_2()
        ...
        self.create_test_data_method_99()

class TestCase_1(..., OneOfTheBaseTest):
        def setUp(self):
            # do something
            super().setUp()
            # do something more
        def test_00_test_some_things(self):
            # tests
        ...
        def test_99_test_other_thigns(self):
            # tests

Those. there is a basic test with a bunch of methods for creating various sets of models, which in setUp fills the database with the necessary initial data. Tests inherit from it and extend the setUp behavior by doing additional customization before or after super().setUp(). The problem is that there is a lot of initial data, OneOfTheBaseTest.setUp takes a long time and does not make much sense - it just fills the database thousands of times with the same data - as a result, the tests take half a day. Django has setUpTestData to get rid of this problem, but it involves a complete rewrite of all methods, and in this case this is not an option at all. How to get around this problem? Is there some kind of decorator that makes setUp() behave like setUpTesData?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question