- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想做的是这个...
x = MagicMock()
x.iter_values = [1, 2, 3]
for i in x:
i.method()
我正在尝试为此函数编写单元测试,但我不确定如何在不调用某些外部资源的情况下模拟所有调用的方法...
def wiktionary_lookup(self):
"""Looks up the word in wiktionary with urllib2, only to be used for inputting data"""
wiktionary_page = urllib2.urlopen(
"http://%s.wiktionary.org/wiki/%s" % (self.language.wiktionary_prefix, self.name))
wiktionary_page = fromstring(wiktionary_page.read())
definitions = wiktionary_page.xpath("//h3/following-sibling::ol/li")
print definitions.text_content()
defs_list = []
for i in definitions:
print i
i = i.text_content()
i = i.split('\n')
for j in i:
# Takes out an annoying "[quotations]" in the end of the string, sometimes.
j = re.sub(ur'\u2003\[quotations \u25bc\]', '', j)
if len(j) > 0:
defs_list.append(j)
return defs_list
编辑:
我不确定是否滥用了模拟。我试图在不调用外部服务的情况下对这个 wiktionary_lookup
方法进行单元测试...所以我模拟了 urlopen..我模拟了 fromstring.xpath() 但据我所知我还需要迭代xpath()
的返回值并调用方法“text_contents()
”,这就是我在这里要做的。
如果我完全误解了如何对这个方法进行单元测试,那么请告诉我哪里出了问题......
编辑(添加当前单元测试代码)
@patch("lang_api.models.urllib2.urlopen")
@patch("lang_api.models.fromstring")
def test_wiktionary_lookup_2(self, fromstring, urlopen):
"""Looking up a real word in wiktionary, should return a list"""
fromstring().xpath.return_value = MagicMock(
content=["test", "test"], return_value='test\ntest2')
# A real word should give an output of definitions
output = self.things.model['word'].wiktionary_lookup()
self.assertEqual(len(output), 2)
最佳答案
您真正想要做的不是返回一个带有 return_value=[]
的 Mock
。您实际上想要返回 Mock
对象的 list
。以下是包含正确组件的测试代码片段和一个小示例,用于展示如何测试循环中的迭代之一:
@patch('d.fromstring')
@patch('d.urlopen')
def test_wiktionary(self, urlopen_mock, fromstring_mock):
urlopen_mock.return_value = Mock()
urlopen_mock.return_value.read.return_value = "some_string_of_stuff"
mocked_xpath_results = [Mock()]
fromstring_mock.return_value.xpath.return_value = mocked_xpath_results
mocked_xpath_results[0].text_content.return_value = "some string"
因此,剖析上面的代码以解释为解决您的问题所做的工作:
帮助我们测试 for 循环中的代码的第一件事是创建一个模拟对象列表:
mocked_xpath_results = [Mock()]
然后,如你所见
fromstring_mock.return_value.xpath.return_value = mocked_xpath_results
我们将 xpath
调用的 return_value
设置为每个 mocked_xpath_results
的模拟列表。
作为如何在列表中执行操作的示例,我添加了如何在循环中模拟,显示为:
mocked_xpath_results[0].text_content.return_value = "some string"
在单元测试中(这可能是一个意见问题)我喜欢明确,所以我明确地访问列表项并确定应该发生什么。
希望这对您有所帮助。
关于python - 可以迭代 MagicMock 对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39783460/
在 Python3.4 中: >>> import mock.MagicMock >>> type(mock.MagicMock()) == mock.MagicMock False # Huh, w
我正在迁移我的项目的 python 版本 (2->3)。测试对 python2 工作正常,但对 python3 提示,错误就像 TypeError: '>' not supported between
为什么“wraps”关键字不能始终对 MagicMock 对象起作用?普通方法传递给包装对象,但不是“特殊”方法。在下面的测试中,第一个断言通过,第二个断言失败。 import mock import
( python 2.6) 我有一个方法: def send_all(self, messages): for message in messages: queue.send(
我运行了一个快速测试,看看是否有什么工作...... >>> from unittest.mock import MagicMock >>> x = MagicMock() >>> x.func.re
假设有人在python文件的头部将模块设置为MagicMock: sys.modules['moduleABC'] = mock.MagicMock() 这会引起麻烦,因为当我尝试运行整个单元测试列表
我有以下配置类: class ConfigB(object): Id = None def __Init__(self, Id): self.Id = Id 它在以下类中实例化并打印属
(Python 2.6) 我在 MagicMock 修补中遗漏了一些东西 给出以下生产代码: class MyQueue(object): def send(self, message):
我想在 Python 测试中模拟一个方法。为此,我需要 MagicMock,我尝试像这样导入它: from unittest import TestCase from unittest.mock im
我想为测试对象模拟一些不重要的函数(其他函数)- time.sleep() 等 我可以用简单的模拟替换它们,这会起作用。但是我希望它在使用错误数量的参数调用时报告错误,没有命名参数等。 有什么方法可以
我想做的是这个... x = MagicMock() x.iter_values = [1, 2, 3] for i in x: i.method() 我正在尝试为此函数编写单元测试,但我不确
我正在设置一个 MagicMock 实例,使用不同的参数调用相同的方法两次,并将我的断言设置为仅验证一组参数。 Python:3.5.2 from unittest.mock import Magic
我是使用 MagicMock 在 python 中进行单元测试的新手。我有以下代码来断言 python 中正确的方法计数: def methodFoo(self): for booOb
在 Python 中,如果您调用一个不存在的方法,它会抛出一个 AttributeError。例如 >>> class A: ... def yo(self): ...
例如: import mock class MyClass(object): def foo(self, x, y, z): return (x, y, z) class Te
mock( http://mock.readthedocs.org/en/latest/index.html ) 是 python 中用于单元测试的好工具。它可以方便地模拟一个方法或类或字典。但是我遇
我们正在模拟一个 3rd 方模块,它有一个带有许多嵌套实例的大型 api。它的各种对象通常通过属性、get() 或字典索引 [] 访问。拥有一个将所有 3 个都视为简单属性访问的灵活模拟将非常方便。
我们正在模拟一个 3rd 方模块,它有一个带有许多嵌套实例的大型 api。它的各种对象通常通过属性、get() 或字典索引 [] 访问。拥有一个将所有 3 个都视为简单属性访问的灵活模拟将非常方便。
我无法让我的测试按预期运行。我试图模拟两个属性的返回值和一个测试的方法,但我没有得到所需的返回值,而是得到了一个 MagicMock 对象。 我不知道该怎么做。最让我恼火的是,模拟该方法可以按预期工作
我正在尝试为 Readthedocs 自动生成文档。我将一些依赖项模拟为 they suggest in FAQ ,但是从函数的类型注释中,我得到了文档的某些部分,看起来像 Return type:
我是一名优秀的程序员,十分优秀!