While working on unittests for python application I faced interesting case when mock.patch
method are used as decorator to patch method. See simplified example and description of problem below:
import unittest
import mock
from app import utils as app_utils
class TestA(unittest.TestCase):
def setUp(self):
pass
@mock.patch('app.utils.method_b', return_value=None)
def test_method_a(self, mock_method_b):
actual_result = app_utils.method_a()
# Assertion code
def test_method_b(self, *args):
actual_result = app_utils.method_b()
# Assertion code
Pay attention that test_method_a
mocking app_utils.method_b
and test_method_b
are going to call original app_utils.method_b
. I faced situation that test_method_b
cannot call actual app_utils.method_b
because it's mocked by test_method_a
.
I knew few methods of how to resolve that problem:
- use
from app.utils import test_method_b
, - so method would leave in namespace of my test.py - use
mock.patch
as context manager viawith
statement.
The question is whether that possible to resolve problem without applying solutions above?
I need to keep using mock.patch
as decorator and keep imports as is, if possible.
Aucun commentaire:
Enregistrer un commentaire