[Answer]-How to mock up a class for several tests in Django

1๐Ÿ‘

โœ…

If you need to do patch for all the tests you can do this in setUpClass method:

class RemoteServiceTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.patchers = []
        patcher = patch('application.PushService.trigger_event')
        cls.patchers.append(patcher)
        trigger_mock = patcher.start()
        trigger_mock.return_value = 'Some return value'

    @classmethod
    def tearDownClass(cls):
        for patcher in cls.patchers:
            patcher.stop()

    def test1(self):
        # Test actions

    def test2(self):
        # Test actions

    def test3(self):
        # Test actions

setUpClass is called once per class (test suite in this case). Inside this method you can set all patchers that you need to be used by all tests.

๐Ÿ‘คWhiteAngel

Leave a comment