Pytest mocker补丁未打补丁

2024-05-17 06:58:32 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使用pytestmocker.patch文件然而,似乎这个补丁并没有实际应用到我的任何测试中。我是不是用错了mocker?你知道吗

测试/测试_单位.py你知道吗

import pytest

def test_get_item(self, client, mocker):
        """Tests get api can get item by ID"""
        item = [{'id': 1,
                 'data': 'test'}]
        patch = mocker.patch('helper.import_item_id',
                             return_value=item)
        id = item[0]['id']

        with application.test_request_context():
            res = client.get(url_for('get_item', id=id))
            result = json.loads(res.get_data())

            patch.assert_called_with(id)
            assert res.status_code == 200

你知道吗助手.py(在根项目文件夹中)

def import_item_id(id):
    """Imports CSV data to df and returns
   a item list' for a given id"""
    items = import_data()
    this_recipe = items.loc[items['id'] == id]
    return this_item.to_dict('records')

它的运行就像没有应用mock一样,即assert_called_with fails,响应状态代码是200。你知道吗


Tags: pytestimportclientiddatagetdef