Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pytest mock模拟对象和对象方法_Python_Unit Testing_Pytest_Pytest Mock - Fatal编程技术网

Python 使用pytest mock模拟对象和对象方法

Python 使用pytest mock模拟对象和对象方法,python,unit-testing,pytest,pytest-mock,Python,Unit Testing,Pytest,Pytest Mock,我正在尝试使用pytestmock进行模拟。该库本质上是mock和patch的插件/包装器 我的问题定义为: 我有一个使用SQL炼金术的应用程序(mymodule.py)。基本上,有一个函数定义SQL Alchemy中的一些对象,并返回包含这些对象的字典 def some_function1(): # some code from sqlalchemy import create_engine, MetaData, Table engine = create_engin

我正在尝试使用
pytestmock
进行模拟。该库本质上是
mock
patch
的插件/包装器

我的问题定义为:

我有一个使用SQL炼金术的应用程序(
mymodule.py
)。基本上,有一个函数定义SQL Alchemy中的一些对象,并返回包含这些对象的字典

def some_function1():
    # some code
    from sqlalchemy import create_engine, MetaData, Table

    engine = create_engine(f"mysql+pymysql://{username}:{password}@{host}:{port})
    meta = MetaData(engine)
    my_table = Table(
        'my_table',
        meta,
        autoload=True,
        schema="some_schema"
    )
    db_tools = {"engine": engine, "table": my_table}
    return db_tools
然后,第二个函数将该输出字典作为输入并使用它们:

def some_function2(db_tools, data):

    sql_query = db_tools["table"].insert().values(data)
    db_tools["engine"].execute(sql_query)
    # some more code
所以现在我正在编写单元测试,我不想实际与真正的数据库通信。所以我只需要模拟所有与
sqlalchemy
相关的东西。到目前为止,我通过以下操作成功模拟了
创建引擎
元数据

mocker.patch(
    'my_module.create_engine',
    return_value=True
)
mocker.patch(
   'my_module.MetaData',
   return_value=True
)
mocker.patch(
   'my_module.Table',
   return_value=True
)

这使我能够测试
一些功能1
。但是现在我需要测试
一些函数2
,它使用方法或属性
.insert()
.values
.execute()
。如何修补它?

模拟
某些函数1
没有多大好处,因为它只会建立到数据库的连接。它不接受任何输入,只返回指向表和连接的字典。关于
某些函数2
,我们只需在
db\u tools
参数中传入多个
MagicMock
,即可使用

当测试运行时,它返回以下内容

========================================================== test session starts ==========================================================
platform darwin -- Python 3.7.4, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: ***
plugins: mock-3.2.0
collected 1 item                                                                                                                        

test_foo.py .                                                                                                                     [100%]

=========================================================== 1 passed in 0.01s ===========================================================
========================================================== test session starts ==========================================================
platform darwin -- Python 3.7.4, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: ***
plugins: mock-3.2.0
collected 1 item                                                                                                                        

test_foo.py .                                                                                                                     [100%]

=========================================================== 1 passed in 0.01s ===========================================================