I have this testing code in Python using mocks (simplified):
from unittest import TestCase
from mock import patch
class TestClass(TestCase):
@patch("mymodule.function1")
@patch("mymodule.function2")
@patch("mymodule.function3")
def test_case_1(self, function3_mock, function2_mock, function1_mock):
# code for test_case_1
...
@patch("mymodule.function1")
@patch("mymodule.function2")
@patch("mymodule.function3")
def test_case_2(self, function3_mock, function2_mock, function1_mock):
# code for test_case_2
...
@patch("mymodule.function1")
@patch("mymodule.function2")
@patch("mymodule.function3")
def test_case_3(self, function3_mock, function2_mock, function1_mock):
# code for test_case_3
...
...
I wonder if there is some way of simplifying this, so:
@patch(...)
statements in each test case functiondef test_case_1(self)
for instanceIs that possible? Could you provide some hints/advice?
Thanks in advance!
Refactored this way achieves both objectives:
from unittest import TestCase
from mock import patch, Mock
function1_mock = Mock()
function2_mock = Mock()
function3_mock = Mock()
@patch("mymodule.function1", function1_mock)
@patch("mymodule.function2", function2_mock)
@patch("mymodule.function3", function3_mock)
class TestClass(TestCase):
def test_case_1(self):
# code for test_case_1
...
def test_case_2(self):
# code for test_case_2
...
def test_case_3(self):
# code for test_case_3
...
...