简体   繁体   English

python单元测试补丁模拟方法不返回返回值

[英]python unit test patch mock method not returning the return values

I am trying to write a test case test_is_user_present() which calls another function execute_redshift_sql() from redshift_util.py script我试图写一个测试用例test_is_user_present()其调用另一个函数execute_redshift_sql()redshift_util.py脚本

I set the expected return value from the function execute_redshift_sql() to 1 .我将函数 execute_redshift_sql() 的预期返回值设置为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)

(you have to substitute redshift_test for the name of your test module in the patch decorator) (您必须在patch装饰器中用redshift_test替换您的测试模块的名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM