python unit test patch mock method not returning the return values

时光总嘲笑我的痴心妄想 提交于 2021-01-28 05:42:30

问题


I am trying to write a test case test_is_user_present() which calls another function execute_redshift_sql() from redshift_util.py script

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

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

回答1:


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)



来源:https://stackoverflow.com/questions/60882117/python-unit-test-patch-mock-method-not-returning-the-return-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!