问题标签 [python-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 投票
5 回答
63661 浏览

python - 在 python 单元测试中模拟类属性的更好方法

我有一个定义类属性的基类和一些依赖它的子类,例如

我想用不同的分配对这个类进行单元测试,例如空字典、单个项目等。这当然非常简化,这不是重构我的类或测试的问题

我最终提出的(pytest)测试是

这感觉相当复杂和 hacky - 我什至不完全理解它为什么工作(虽然我熟悉描述符)。mock 是否会自动将类属性转换为描述符?

感觉更合乎逻辑的解决方案不起作用:

要不就

我尝试过的其他变体也不起作用(作业在测试中保持不变)。

模拟类属性的正确方法是什么?有没有比上述方法更好/更容易理解的方法?

0 投票
3 回答
8654 浏览

python - 检查多个模拟的调用顺序

我有三个函数正在尝试测试其调用顺序。

假设在模块 module.py 我有以下内容

我想检查 b 在 a 之后和 c 之前是否被调用。因此,为 a、b 和 c 中的每一个进行模拟很容易:

检查每个单独的模拟是否被调用也很容易。如何检查通话相对于彼此的顺序?

call_args_list不起作用,因为它是为每个模拟单独维护的。

我尝试使用副作用来记录每个调用:

但这只会给我调用模拟的参数,而不是调用所针对的实际模拟。我可以添加更多逻辑:

这似乎可以完成工作......但是有更好的方法吗?感觉 API 中应该已经有一些我缺少的东西可以做到这一点。

0 投票
1 回答
3555 浏览

python - 使用补丁在 Python 中模拟 Celery 任务调用

使用模拟的返回值修补 Celery 任务调用,<Mock name='mock().get()' ...>而不是return_valuemock_task.get.return_value = "value". 但是,模拟任务在我的单元测试中正常运行。

这是我正在修补 Celery 任务的单元测试:

这是正在测试的功能:

最后一行加注TypeError: 'Mock' object has no attribute '__getitem__'

为什么我可以在我的单元测试中调用 mock_task.get() ,但调用它会foo返回 a<Mock ...>而不是预期的返回值?

0 投票
2 回答
457 浏览

python - 是否可以将 python mock 用于复杂的多维字典输入?

我已经开始阅读有关模拟库的信息,但还没有完全弄清楚如何将它用作输入值来测试我的同步功能。

我的同步函数从外部源获取多维字典,然后对其进行解析并转换为各种 Django 数据库记录

我勇敢地尝试过:

但由于模拟返回的值的类型,肯定会直接失败。

所以我认为我最好手动设置一些返回值,我尝试了以下实验:

还尝试了 return_value

但是同步函数中的代码需要字典中的整数值......

0 投票
1 回答
1322 浏览

python - python mock patch 顶级包

在 python 中使用mock,无法直接修补顶级包(如 argparse)——大概是因为没有对 patch 的引用。一种解决方案是将每个单独的调用修补到包中(如 argparse.ArgumentParser)。理论上,如果在没有相应补丁的情况下添加对包的调用,这将导致在单元测试期间意外调用导入。

尽管解决方法并不是那么不方便,但最好直接修补 argparse 之类的软件包。mock_open似乎使用了一些恶作剧来修补内置open()using __main__.open. 这是如何工作的,是否可以使用类似的恶作剧来修补顶级软件包?

0 投票
1 回答
1696 浏览

python - Best way of patching datetime in Django unit tests

I am using a home brew datetime.datetime mock to patch out datetime throughout the code (see at the very bottom), but other people seem to hit problems understanding how it works, and hit unexpected problems. Considered the following test:

Now if you look at how Django DatetimeField serializes dates to SQL:

Source

This part gets executed as you call u.save() in the test. As this point in the Django code value of value (u.last_login) is of type datetime.datetime because we've assigned the value in the test using an unpatched version of datetime (since our import is at the module level, and the patch is at the method level).

Now in the Django code, datetime.datetime is patched, therefore:

is equivalent to:

which is False, but:

is True hence the datetime.datetime object gets converted to a datetime.date, and when you retrieve u2.last_login from the SQL, the value is actually datetime(2014, 04, 01, 0, 0, 0) and not datetime(2014, 04, 01, 14, 0, 0)

Hence the tests fail.

The way around this is to replace:

with:

but this seems prone to mistakes and tends to confuse people using or writing the tests.

Especially in cases where you need the real now value you have to either do datetime_to_fakedatetime(datetime.datetime.now()) or call FakeDatetime.now() but make sure that the previous test has unset the FakeDatetime.now_value.

I am looking for a way to make this more intuitive, but at the same time avoid having to patch the datetime.datetime objects in particular sub modules (as there might be many of them), and just patch it throughout the code.

Code for the homebrew mock:

Thanks!

0 投票
1 回答
703 浏览

python - 断言 *any* 属性已设置

我有一个 Python模拟对象,我想断言该对象的任何属性是否已设置。

我不相信PropertyMock会为我的目的工作,因为我必须知道是否设置了任何属性,而不是特定的属性。

看起来我也不能模拟__setattr__模拟对象的方法。

如何测试是否设置了模拟对象的任意属性?

0 投票
1 回答
2832 浏览

python - python mock和magic mock有什么区别?

python mock和magic mock有什么区别,因为我可以看到

何时使用 mock obj 以及何时使用 MagicMock obj?

0 投票
1 回答
5112 浏览

python - python模拟第三方模块

我试图测试一些处理推文的类。我使用 Sixohsix twitter 来处理 Twitter API。我有一个类作为 Twitter 类的外观,我的想法是模拟实际的 Sixohsix 类来模拟推文的到来,方法是随机生成新推文或从数据库中检索它们。

我的外观看起来像:

因此,如果在单元测试中我想测试一些对推文做某事的模块,我将如何模拟 TwitterStream 类?我试过使用模拟:

这不起作用,仍然调用 twitter api。我原以为我没有在模拟类中添加任何代码,我会得到一个错误或其他东西,而是调用了六个 twitter 类。

0 投票
1 回答
1244 浏览

python - 模拟:跟踪所有呼叫。外壳与程序不一致

为什么 mock.mock_calls 的内容会根据我是在 shell 中运行还是在程序中运行而有所不同?断言mock.mock_calls == expected在程序中失败,但在 shell 中通过。该代码基于用于跟踪多个呼叫的文档。(26.5.1.5. 跟踪所有呼叫)

到目前为止,这一切都完全符合预期。但是看看mock.mock_calls程序中发生了什么:它记录了__str__对每个被调用方法的方法的额外调用。mock.method_calls 不受影响。

输出:

我在 Windows 7 计算机上运行 Python 3.4。