如何使用Mock库修补Python类

时间:2012-06-22 11:20:32

标签: python unit-testing mocking scrapy python-mock

我在修补课程时遇到了麻烦。我正在尝试修补属于Scrapy的东西 - HtmlXpathSelector类。

以下是一些代码:

from scrapy.selector import HtmlXPathSelector
from mock import MagicMock, patch

with patch('scrapy.selector.HtmlXPathSelector') as MockHtml:
    instance = MockHtml.return_value
    instance.method.return_value = 'foo'
    example = HtmlXPathSelector()
    print type(example)
    assert example is instance
    assert example.method == 'foo'

结果是:

<class 'scrapy.selector.lxmlsel.HtmlXPathSelector'>
Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
AssertionError
>>>

此示例与Mock库教程中的示例相同。知道为什么它不起作用吗?

2 个答案:

答案 0 :(得分:6)

您不应修补当前测试代码中已导入的类。相反,您需要在相应的模块中修补该类(您想要测试)。因此,如果HtmlXPathSelector中导入mymodule,您将修补为:

with patch('mymodule.HtmlXPathSelector') as MockClass:
    ...

有关详细信息,请参阅where to patch

编辑如果您真的需要这个,可以使用以下方法修补当前模块中的类:

with patch('__main__.Class') as MockClass:

答案 1 :(得分:2)

您的代码示例存在两个问题。第一个是您从scrapy模块导入了HtmlXPathSelector,然后在事后更改了该名称。改为导入选择器,并使用其中的名称:

from scrapy import selector
from mock import MagicMock, patch

with patch('scrapy.selector.HtmlXPathSelector') as MockHtml:
    instance = MockHtml.return_value
    instance.method.return_value = 'foo'
    example = selector.HtmlXPathSelector()
    print type(example)
    assert example is instance
    assert example.method() == 'foo'

另一个问题是,当需要以method方式调用方法时,您的最后一行是检查method()