Reputation: 135
I have the following unit test in Python (using Python 3.8):
def test_Wrapper_permanent_cookie_OK_True(self):
# Some stuff
mock_3 = PermanentCookie
mock_3.create_permanent_cookies_response = mock.Mock(return_value=response)
# The following line uses this mock on its execution
result = permanentcookies.Wrapper_permanent_cookie(Input)
# Test passes (response gets defined previously)
self.assertTrue(result == (response, None))
The issue starts when the following test is executed. It is the test to the function I mocked previously.
def test_create_permanent_cookies_response(self):
permanentcookies = PermanentCookie()
result = permanentcookies.create_permanent_cookies_response(status2, error2)
# Test does not pass because "result" is not the execution of the function but the mock from the previous test (response gets defined previously)
self.assertTrue(result == response)
Any suggestions on how to completely remove the previous mock/isolate each test from the rest/...?
Thanks in advance!
-------------------------------------------------------EDIT-------------------------------------
My test function is using patch method. But in these patches, there is a class that I need to test the function. Maybe I'm missing something basic on patching classes... My code:
@mock.patch('src.servicios.permanent_cookies.PermanentCookie')
@mock.patch('src.servicios.permanent_cookies.utilities.get_current_datetime')
@mock.patch('src.servicios.permanent_cookies.queries.query_permanent_cookie')
def test_Wrapper_permanent_cookie_OK_True(self, mock_1, mock_2, mock_3):
# The following line is not sending my return_value expectation to function usage
mock_3.create_permanent_cookies_response.return_value = 'test'
# This is the usage of the class I mocked on the patch above
permanentcookies = PermanentCookie()
# Unexpected outcome as the method I passed the return_value method did not return that value.
result = permanentcookies.Wrapper_permanent_cookie(Input)
Upvotes: 0
Views: 56
Reputation: 135
It seems the way to patch the function inside a class was first giving the class and then the function on the patch decorator. As long as the patch was on the decorator (and not inside the test as mock.Mock(return_value=...) ), the patch was properly done on the class' function level and not extended further than this test.
For all those arriving at this point as well, from the Edit, the answer was:
@mock.patch('src.servicios.permanent_cookies.PermanentCookie.create_permanent_cookies_response')
@mock.patch('src.servicios.permanent_cookies.utilities.get_current_datetime')
@mock.patch('src.servicios.permanent_cookies.queries.query_permanent_cookie')
def test_Wrapper_permanent_cookie_OK_True(self, mock_1, mock_2, mock_3):
mock_3.return_value = 'Your Value'
permanentcookies = PermanentCookie()
# Now getting the correct
result = permanentcookies.Wrapper_permanent_cookie(Input)
Upvotes: 0
Reputation: 879
Ordinarily, a patch should not extend outside of its current context.
So everything patched in test 1:
@patch(...func1)
def test_test1(sef):
func1() # <- patched.
Will be reset when you come to test 2:
def test_test2(sef):
func1() # <- not patched.
But sometimes, you do need to keep an unpatched version of your function alongside a patched version:
in this case, you can do:
_unpatched = func1
@patch(...func1)
def test_patched_with_unpatched(self):
func1() # <- patched.
_unpatched() # <- func1 not patched.
Upvotes: 1
Reputation: 362786
You're not actually using patch
anywhere. It's using patch which removes the mock at the end of the test.
patch()
acts as a function decorator, class decorator or a context manager. Inside the body of the function or with statement, the target is patched with a new object. When the function/with statement exits the patch is undone.
Upvotes: 1