使用 Pytest 在函数中模拟引发异常

问题描述 投票:0回答:2

我有以下功能

get_postgres_connection
。我正在尝试使用
test_get_postgres_connection_unsuccess
运行单元测试来解决异常。

def get_postgres_connection():
        
        try:
            conn = psycopg2.connect(dbname = POSTGRES_DATABASE,
                                user = POSTGRES_USER,
                                password = POSGRES_PASSWORD,
                                host = POSTGRES_HOST,
                                port = POSTGRES_PORT)
            conn.autocommit = True
            
            return conn
            
        except Exception as err:
            logging.error('Unable to connect to postgres db: %s', err)


def test_get_postgres_connection_unsuccess(monkeypatch):
        """ tests an exception is hit if the connection to postgres is unsuccesful"""
        
        # setup
        class mock_conn:
            def __init__(self, dbname, user, password, host, port):
                raise ConnectionError('This fake connection did not work')

            autocommit = False

        monkeypatch.setattr(psycopg2, 'connect', mock_conn)

我无法在模拟函数中成功引发异常。有人知道我在这里做错了什么吗?

编辑:稍微清理一下代码

python postgresql exception mocking pytest
2个回答
5
投票

无需创建自己的模拟类 - 使用

unittest.mock.MagicMock
代替。

您可以使用

MagicMock
实例来模拟任何东西,包括第三方函数。如果添加
side_effect=Exception()
参数,则调用模拟时会引发异常。

Python 甚至允许您在上下文管理器中执行此操作(

with ...
语句),以便一旦上下文管理器块结束,模拟函数就会“取消模拟”。

最小示例:

def some_external_lib():  # this is psycopg2.connect in your example
    pass

def my_func():  # this is get_postgres_connection in your example
    try:
        some_external_lib()
    except Exception as e:
        print(f"Error found: {e}")


import unittest
from unittest import mock

class TestMyFunc(unittest.TestCase):
    def test_my_func_external_lib_raises_exception(self):
        with mock.patch('__main__.some_external_lib', side_effect=Exception("ERROR")):
            my_func()


# Running example - prints the error message
t = TestMyFunc()
t.test_my_func_external_lib_raises_exception()

请注意,正如所编写的那样,测试现在实际上并没有测试任何内容。查看

get_postgres_connection
函数的主体,您可能想测试它是否返回
None
,以及在外部库引发异常的情况下是否将某些内容写入日志文件。


2
投票

这是我如何使用模拟修复测试功能,如上面的回答

    def test_get_postgres_connection_unsuccess():
        """ tests an exception is hit if the connection to postgres is unsuccesful"""

        # assert
        with mock.patch('psycopg2.connect', side_effect=Exception('ERROR')):
            self.pg_db.get_postgres_connection()
© www.soinside.com 2019 - 2024. All rights reserved.