测试 A mock 被带到测试 B
Test A mock is being taken to Test B
我在 Python 中进行了以下单元测试(使用 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))
执行以下测试时问题开始。这是对我之前模拟的功能的测试。
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)
关于如何从其余测试中完全删除之前的 mock/isolate 每个测试的任何建议/...?
提前致谢!
-------------------------------------------- ----------编辑---------------------------- --------
我的测试函数是使用补丁方法。但是在这些补丁中,有一个 class 是我需要测试的功能。也许我遗漏了一些关于修补 classes 的基本知识...我的代码:
@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)
您实际上并没有在任何地方使用 patch
。它使用 删除 测试结束时的模拟的补丁。
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.
通常,补丁不应扩展到其当前上下文之外。
所以测试 1 中的所有补丁:
@patch(...func1)
def test_test1(sef):
func1() # <- patched.
当你来测试2时会被重置:
def test_test2(sef):
func1() # <- not patched.
但有时,您确实需要将函数的未打补丁版本与打补丁的版本一起保留:
在这种情况下,您可以这样做:
_unpatched = func1
@patch(...func1)
def test_patched_with_unpatched(self):
func1() # <- patched.
_unpatched() # <- func1 not patched.
似乎在 class 中修补函数的方法是首先给出 class,然后是修补装饰器上的函数。只要补丁在装饰器上(而不是在 mock.Mock(return_value=...) 的测试中),补丁就在 class' 功能级别上正确完成并且没有比这个测试更进一步。
对于所有到达这一点的人,编辑的答案是:
@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)
我在 Python 中进行了以下单元测试(使用 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))
执行以下测试时问题开始。这是对我之前模拟的功能的测试。
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)
关于如何从其余测试中完全删除之前的 mock/isolate 每个测试的任何建议/...?
提前致谢!
-------------------------------------------- ----------编辑---------------------------- --------
我的测试函数是使用补丁方法。但是在这些补丁中,有一个 class 是我需要测试的功能。也许我遗漏了一些关于修补 classes 的基本知识...我的代码:
@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)
您实际上并没有在任何地方使用 patch
。它使用 删除 测试结束时的模拟的补丁。
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.
通常,补丁不应扩展到其当前上下文之外。
所以测试 1 中的所有补丁:
@patch(...func1)
def test_test1(sef):
func1() # <- patched.
当你来测试2时会被重置:
def test_test2(sef):
func1() # <- not patched.
但有时,您确实需要将函数的未打补丁版本与打补丁的版本一起保留:
在这种情况下,您可以这样做:
_unpatched = func1
@patch(...func1)
def test_patched_with_unpatched(self):
func1() # <- patched.
_unpatched() # <- func1 not patched.
似乎在 class 中修补函数的方法是首先给出 class,然后是修补装饰器上的函数。只要补丁在装饰器上(而不是在 mock.Mock(return_value=...) 的测试中),补丁就在 class' 功能级别上正确完成并且没有比这个测试更进一步。
对于所有到达这一点的人,编辑的答案是:
@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)