问题标签 [pytest-mock]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
2242 浏览

python - 如何使用 pytest-mock 或 magicmock 模拟导入的对象

我正在尝试了解这些mock/monkeypatch/pytest-mock功能。

让我知道这是否可能。如果不能,请建议我如何测试此代码。

我的代码结构:

/app/__init__.py是我的应用程序(如果有帮助的话是 Flask 应用程序)与初始化数据库连接对象到 MongoDB 数据库的地方:

some_module1some_module导入对象并将其db_conn用作其功能的一部分:

在我的测试中,我想根据从数据库接收到的数据来测试我的代码是否正常工作。我想模拟数据库,更具体地说是db_conn对象,因为我不想使用真正的数据库(这将是设置环境和维护它的大量工作)。

关于如何模仿的任何建议db_conn

我一直在探索pytest-mockmagicmock但我不知道也不知道如何db_conn在我的测试中模拟。

0 投票
1 回答
6245 浏览

python-3.x - 从具有副作用的类方法引发的模拟异常给出“没有引发”

注意:此问题基于我之前提出的问题,但根据此答案修改为不同。

使用side_effect,我试图在'URLError'调用模拟时引发异常,但我收到一个DID NOT RAISE我不理解的错误。

我有一个Query带有类方法的类,make_request_and_get_response它可以引发几个异常。我没有在 in 的方法中捕获'URLError'异常,所以我应该能够期望被引发,然后,它的异常被嘲笑。get_response_from_external_apimain.py

查询.py

主文件

使用pytest,我试图模拟那个“危险”的方法 ( make_request_and_get_response),它对特定的异常有副作用。然后,我继续创建一个模拟Query对象以在调用时使用,make_request_and_get_response并期望最后一次调用会给出“URLError”异常。

test_main.py

上面的测试给出了以下错误:

即使我捕获'URLError'异常然后在get_response_from_external_api.

有人可以帮助我了解我缺少什么以便能够在 pytest 中引发异常吗?


根据@SimeonVisser 的回复更新:

如果我修改main.py以删除except Excpetion案例:

然后测试test_main.py

测试通过 OK。

0 投票
1 回答
1920 浏览

python-3.x - 如何模拟引发 urllib 错误

在python 文档中阅读此内容后,我发现了(通过调用)可以引发HTTPErrorURLError异常:get_response_from_external_apimake_request_and_get_responseurlliburlopen

foo.main.py

在测试该get_response_from_external_api方法时,我试图模拟引发HTTPErrorURLError异常

foo.test_main.py

但我得到一个TypeError: __init__() missing 5 required positional arguments: 'url', 'code', 'msg', 'hdrs', and 'fp'. 我是 python 模拟语法的新手,正在寻找示例。

我已经阅读了这个答案,但我看不到如何在我的情况下应用它,其中urllib.urlopen(via get_response_from_external_api) 的返回值超出了 except-block 的范围。不知道我是否应该像这里urllib.urlopen.read看到的那样模拟整个?

0 投票
1 回答
786 浏览

python-3.x - 使用 pytest,我如何模拟 pathlib 的 Path.isdir() 函数和 os.listdir

我正在尝试为此功能编写一个 pytest 测试。它会查找以一天命名的文件夹。

模拟 os.listdir 工作正常。(当我关闭 isdir 检查时)

同时模拟 pathlib 的路径不起作用:

我得到错误:

如何在同一个调用中同时模拟 listdir 和 Path?

0 投票
1 回答
957 浏览

python - Python module pytest-mock how to test method call arguments when one class call another class instance?

I started out learning unit testing using pytest a couple of days ago, and got interested in the pytest-mock plugin (https://github.com/pytest-dev/pytest-mock).

I have been able to write quite a lot of unit tests to test my code, but now I would like to test how parts of my code interact with other objects. Let's say that I have class B, which is the class that I am interested in testing, that have a method that will call a method in class A, and I would like to assert that the method call was made with the expected arguments when class B make the call to class A.

I put together some example hack (see the link below) that will get the job done, but as you can see this is probably not the proper way of doing things. So my question is, how is this properly handled by using the Python mock or pytest-mock modules? Answers not based on the pytest-mock plugin are appreciated as well.

In the code below it is the assert that I am not happy about, since I would prefer to use the existing pytest-mock "assert_called_once_with(...)"-method, but I just can't get it to work. All information is available in the mock-object, but I don't understand how to properly use the pytest-mock API in this case.

0 投票
1 回答
3277 浏览

python - Python pytest-mock assert_has_calls

我正在研究名为 pytest-mock ( https://github.com/pytest-dev/pytest-mock ) 的优秀 pytest 插件,现在我正在尝试一些带有 assert_has_calls 的示例。简而言之,我正在测试 B 类的一个实例,更具体地说,该实例如何与 A 类的实例交互(我在其中模拟了“time_sumption_task”方法)。

该示例正在使用 alt。B(见代码中的注释)。我更喜欢alt。A,而是直接mock A类中的方法,而不是mock通过B类的实例(obj)访问的A类实例中的方法。

0 投票
1 回答
3150 浏览

python - Pytest lambda 处理程序传递事件和上下文

我正在使用 pytest 为我的 lambda 函数编写单元测试。我不知道应该如何将我的事件参数传递给函数调用。我了解到它可以使用@pytest.fixture 来实现。我对 Python 和 pytest 非常陌生。相信我以错误的方式使用固定装置。请帮我!!

下面是我的 lambda 处理程序:

lambda_service.py

这是我的测试课-

运行此测试时出现以下错误

虽然我在固定装置中传递事件和上下文,但它仍然指的是event[sort]inside lambda_handler

0 投票
1 回答
99 浏览

python - 每次调用后更改值的 PropertyMock

我正在尝试模拟 psycopg2 并需要处理 cursor.description

我可以通过 PropertyMock 做到这一点:

但这并没有解决我的问题,因为被测系统进行了两次查询,并且第二次描述会有所不同。

如何在第二次调用时更改模拟的返回值?

我试过了:

但我明白了

(这对我来说没有意义。一定有一个奇怪的范围界定问题。)

我最终确实让它与这段代码一起工作,但似乎必须有更好的方法:

0 投票
1 回答
1067 浏览

python-3.x - 如何模拟第三方静态方法

为了简化我的问题,请考虑以下代码:
How do you write test for function foo1with patching\mocking the S3Downloader.read_filepart?

我更喜欢你会向我展示pytest-mock甚至的示例用法unitest.mock

0 投票
0 回答
728 浏览

python - 使用 pytest 和 pytest-mock 使用 json 参数模拟 Flask POST 请求

我想用烧瓶测试 POST 请求的输入/输出,如果可能的话,可能会模拟这两种结果。为简单起见,我已经剥离了大部分逻辑。

我对这种情况不满意,因为一切都是模拟的,我不能保证函数会正常运行。理想情况下,我能够为 POST 请求传递 JSON 数据,并jsonify(result)检查extract.py.

我这样做的通常方法是调用request.get('/extract', json=json.dumps(data))并测试提供的响应,因为这实际上会运行代码。问题是,我知道这并不理想,因为如果服务器出现故障或其他问题,我将无法正确测试它。