【发布时间】:2019-12-31 01:19:12
【问题描述】:
我在获取 pytest、相关导入和补丁以配合使用时非常开心。
我有以下几点:
from .. import cleanup
class CleanupTest(unittest.TestCase):
@patch.object(cleanup, 'get_s3_url_components', MagicMock())
@patch.object(cleanup, 'get_db_session', MagicMock(return_value={'bucket', 'key'}))
@patch('time.time')
def test_cleanup(self, mock_time, mock_session, mock_components):
...
我尝试了一些变化。
- 当前显示的一个。 Pytest 返回“TypeError:test_cleanup() 正好需要 4 个参数(给定 2 个)”。它无法识别patch.objects,即使我很确定我根据https://docs.python.org/3/library/unittest.mock-examples.html正确使用它们
- 将 patch.objects 更改为简单的补丁会导致它们抛出“没有名为 cleanup 的模块”。我不知道如何将补丁与相对导入一起使用。
编辑:我正在使用 Python 2.7,以防万一。
【问题讨论】:
-
这看起来是正确的 - 也许您使用的是旧版本的
pytest无法识别模拟? -
我检查了这个。 pytest 确实可以识别我正在使用的其他模拟。
-
哦,我仔细阅读后明白了,它与 pytest 无关——让我写一个答案