gpt4 book ai didi

python - 使用 mock 模拟导入的模块以测试 Pylons Controller 功能

转载 作者:行者123 更新时间:2023-12-05 08:00:17 24 4
gpt4 key购买 nike

我一直在追寻一个问题,即实现用于测试 Pylons Controller 功能的模拟。

我正在尝试测试的特定功能可以在这里找到:https://hg.mozilla.org/build/buildapi/file/efe11511f42d/buildapi/controllers/selfserve.py#l210

目标:

我只是想模拟它调用的函数,以便我可以确认它最终调用了 get_completeness。

问题:

我可以很好地模拟 _ok、_get_stable_delay、_failed 和 _branches_cache。但是当涉及到 g.buildapi_cache.get_builds_for_revision 时,我似乎无法正确模拟它,每次我调用 revision() 时,它都会失败。

g 实际上是 pylons.app_globals 并像这样导入到 selfserve.py Controller 中:'from pylons import app_globals as g'

测试:

我正在使用 Python 解释器来测试所有这些。这些是我正在尝试的 2 个实现:http://pastebin.mozilla.org/2931825http://pastebin.mozilla.org/2931832

总而言之,这里是解释器中的完整运行,包括 http://pastebin.mozilla.org/2931825 的 Traceback。

>>> from buildapi.controllers.selfserve import SelfserveController
>>> from mock import patch, Mock
>>> s = SelfserveController()
>>> s._ok = Mock(name='_ok')
>>> s._get_stable_delay = Mock(return_value=180)
>>> s._branches_cache = Mock()
>>> s._branches_cache = {'try': None}
>>> s._failed = Mock(return_value="Failed")
>>> with patch('buildapi.controllers.selfserve.g.buildapi_cache.get_builds_for_revision') as get_builds_for_revision:
... get_builds_for_revision.return_value = ['baz', 'bang']
... result = revision('try', 'bar')
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/mock.py", line 1252, in __enter__
self.target = self.getter()
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/mock.py", line 1414, in <lambda>
getter = lambda: _importer(target)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/mock.py", line 1102, in _importer
thing = _dot_lookup(thing, comp, import_path)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/mock.py", line 1089, in _dot_lookup
return getattr(thing, comp)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/paste/registry.py", line 137, in __getattr__
return getattr(self._current_obj(), attr)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/paste/registry.py", line 197, in _current_obj
'thread' % self.____name__)
TypeError: No object (name: app_globals) has been registered for this thread

这里是 http://pastebin.mozilla.org/2931832

>>> from buildapi.controllers.selfserve import SelfserveController
>>> from mock import patch, Mock

>>> s = SelfserveController()
>>> s._ok = Mock(name='_ok')
>>> s._get_stable_delay = Mock(return_value=180)
>>> s._branches_cache = Mock()
>>> s._branches_cache = {'try': None}
>>> s._failed = Mock(return_value="Failed")
>>> g = Mock()
>>> g.buildapi_cache = Mock(name='buildapi_cache')
>>> g.buildapi_cache.get_builds_for_revision = Mock(name='get_builds_for_revision', return_value=['baz', 'bang'])
>>> s.revision('try', 'bar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "buildapi/controllers/selfserve.py", line 217, in revision
job_items = g.buildapi_cache.get_builds_for_revision(branch, revision)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/paste/registry.py", line 137, in __getattr__
return getattr(self._current_obj(), attr)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/paste/registry.py", line 197, in _current_obj
'thread' % self.____name__)
TypeError: No object (name: app_globals) has been registered for this thread

这与我在尝试运行修订版时遇到的错误相同。每次它都会在这里出错,因为我尝试模拟这个 app_globals 模块失败了,或者看起来是这样。我真的不知道还有什么办法可以试试这个。 有人有什么想法吗?

这里有一些更好的细节:

注一

我已经查看了以下似乎不太适用于此特定场景的线程,或者我完全忽略了这一点。

最佳答案

如果没有初始化的 Pylons 环境, Controller 不能用作独立对象。

使用 Pylons 提供的测试功能可以让您免于使用所有样板代码来测试 Controller 。

我建议您将 TestController 类与 nosetests 结合使用,如 documentation 所示。 .

特别是对于您的代码,我认为 Pylons 对待私有(private)方法(以_开头)与公共(public)方法不同,因为公共(public)方法在请求的上下文中运行。

关于python - 使用 mock 模拟导入的模块以测试 Pylons Controller 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18500609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com