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:
- I don't repeat all the time the three
@patch(...)statements in each test case function - I don't need to pass the mock function to the test case function, so I can call write just
def test_case_1(self)for instance
Is that possible? Could you provide some hints/advice?
Thanks in advance!
Refactored this way achieves both objectives: