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

时间:2020-03-27 08:12:39

标签: python unit-testing mocking python-unittest

我正在尝试编写一个测试用例test_is_user_present(),它会从execute_redshift_sql()脚本

调用另一个函数redshift_util.py

我将函数execute_redshift_sql()的期望返回值设置为1。但是在调用函数后,我再也没有从结果中得到这个值!我还打印了一些值用于调试目的

您可以在下面查看测试用例

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 个答案:

答案 0 :(得分:4)

您必须在导入功能时对其进行修补。解决此问题的一种可能性是使用:

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)

在您的情况下,您已经从软件包中模拟了该功能,但是使用了本地导入的模块。您始终必须检查where to patch

上面,我修改了导入,也可以修改补丁:

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代替测试模块的名称)