Creating unit tests for a python project we're reaching this kind of 'template'
from unittest import TestCase
from unittest.mock import patch, Mock
@patch('......dependency1')
@patch('......dependency2')
@patch('......dependencyn')
class MyTest(TestCase):
def test_1(self, mock1, mock2, mockn):
# setup mock1 & mock2...
# call the subject case 1
# assert response and/or interactions with mock1 and mock2...
def test_2(self, mock1, mock2, mockn):
# setup mock1 & mock2...
# call the subject case 2
# assert response and/or interactions with mock1 and mock2...
The point is, sometimes the "setup" section is duplicated across some test cases, so what I'd like to extract configuration to the setUp()
method for example, the following is pseudo-code:
def setUp(self):
mock1.foo.return_value = 'xxx'
mock2.goo.side_effect = [ ... ]
def test_1(self, mock1, mock2, mockn):
# default setup is perfect for this test
def test_2(self, mock1, mock2, mockn):
# this time I need...
mock2.goo.side_effect = [ ... ]
Is it possible to achieve this idea?