python单元测试补丁模拟方法不返回返回值 [英] python unit test patch mock method not returning the return values
问题描述
我正在尝试编写一个测试用例 test_is_user_present()
,它从 redshift_util.py
脚本调用另一个函数 execute_redshift_sql()
I am trying to write a test case test_is_user_present()
which calls another function execute_redshift_sql()
from redshift_util.py
script
我将函数 execute_redshift_sql() 的预期返回值设置为 1
.但是我从来没有在调用函数后从结果中得到这个值!我还打印了一些用于调试目的的值
I set the expected return value from the function execute_redshift_sql() to 1
. But I never get this value returned from result after the function is called ! I also printed some values for debugging purpose
你可以看看下面的测试用例
You can take a look at test case below
from mock import patch, Mock, MagicMock
from cia_admin_operations.redshift_util import execute_redshift_sql
@patch('cia_admin_operations.redshift_util.execute_redshift_sql')
def test_is_user_present(mock_execute_redshift_sql):
ldap_user = "dummy_user"
mock_out = Mock()
user_check_sql = "SELECT COUNT(1) FROM pg_user WHERE usename = '{}';".format(ldap_user)
mock_execute_redshift_sql.return_value = 1
print(mock_execute_redshift_sql())
result = execute_redshift_sql(mock_out, user_check_sql)
print(result)
print(result())
> assert result() == 1
E AssertionError: assert <Mock name='m...749067684720'> == 1
E -<Mock name='mock.query().getresult()()' id='139749067684720'>
E +1
test/test_cia_admin_operations.py:51: AssertionError
----------------------------- Captured stdout call -----------------------------
1
<Mock name='mock.query().getresult()' id='139749067684776'>
<Mock name='mock.query().getresult()()' id='139749067684720'>
redshift_util.py
redshift_util.py
def execute_redshift_sql(connection, sqlQuery):
"""Executes redshift query"""
logger.info("Executing following SQL query :\n %s" % sqlQuery)
try:
result = connection.query(sqlQuery)
logger.info("Redshift query is successfully executed.")
except Exception as e:
logger.error("Query not executed : %s" % e)
return None
# return only if the result has some data
if result:
logger.info("Query result :\n %s" % result)
return result.getresult()
else:
return 0
推荐答案
您必须在导入函数时对其进行修补.解决此问题的一种可能性是使用:
You have to patch the function as it is imported. One possibility to fix this is to use:
from mock import patch, Mock
import cia_admin_operations.redshift_util
@patch('cia_admin_operations.redshift_util.execute_redshift_sql')
def test_is_user_present(mock_execute_redshift_sql):
ldap_user = "dummy_user"
mock_out = Mock()
user_check_sql = "SELECT COUNT(1) FROM pg_user WHERE usename = '{}';".format(ldap_user)
mock_execute_redshift_sql.return_value = 1
result = cia_admin_operations.redshift_util.execute_redshift_sql(mock_out, user_check_sql)
print(result)
在您的情况下,您从包中模拟了该函数,但使用了本地导入的模块.您必须始终检查在哪里打补丁.
In your case, you had mocked the function from the package, but used a locally imported module. You always have to check where to patch.
以上,我适配了导入,你也可以适配补丁:
Above, I adapted the import, you can also adapt the patching:
from mock import patch, Mock
from cia_admin_operations.redshift_util import execute_redshift_sql
@patch('redshift_test.execute_redshift_sql')
def test_is_user_present(mock_execute_redshift_sql):
ldap_user = "dummy_user"
mock_out = Mock()
user_check_sql = "SELECT COUNT(1) FROM pg_user WHERE usename = '{}';".format(ldap_user)
mock_execute_redshift_sql.return_value = 1
result = execute_redshift_sql(mock_out, user_check_sql)
print(result)
(您必须用 redshift_test
替换 patch
装饰器中测试模块的名称)
(you have to substitute redshift_test
for the name of your test module in the patch
decorator)
这篇关于python单元测试补丁模拟方法不返回返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!