在多进程模式下运行nosetests时,未正确处理导入

时间:2015-04-17 09:49:54

标签: python python-2.7 google-app-engine nosetests nose-gae

鉴于测试用例:

import unittest
import mock

class TestTest(unittest.TestCase):
  def test_test(self):
    print dir(__import__('google'))
    with mock.patch('google.appengine.api.urlfetch.fetch'):
      pass

-

$ nosetests --with-gae --processes=0
Ran 1 test in 0.187s
OK

-

$ nosetests --with-gae --processes=1
======================================================================
ERROR: test_test (test_test.TestTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/sadovnychyi/example/test_test.py", line 8, in test_test
    with mock.patch('google.appengine.api.urlfetch.fetch'):
  File "/usr/local/lib/python2.7/site-packages/mock.py", line 1252, in __enter__
    self.target = self.getter()
  File "/usr/local/lib/python2.7/site-packages/mock.py", line 1414, in <lambda>
    getter = lambda: _importer(target)
  File "/usr/local/lib/python2.7/site-packages/mock.py", line 1102, in _importer
    thing = _dot_lookup(thing, comp, import_path)
  File "/usr/local/lib/python2.7/site-packages/mock.py", line 1092, in _dot_lookup
    return getattr(thing, comp)
AttributeError: 'module' object has no attribute 'appengine'
-------------------- >> begin captured stdout << ---------------------
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'net']

知道为什么会发生这种情况以及如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

所以我在nosegae.py中错过了一行:

del sys.modules['google']

已经修复了reload(sys.modules['google'])

https://github.com/Trii/NoseGAE/commit/82fe8b4cb2c037ffd441fa5bed8a6b84a066bbd2

问题已经消失。

答案 1 :(得分:0)

  

AttributeError:'module'对象没有属性'appengine'

这表明GAE SDK未正确定位。

检查您的SDK安装/使用说明,python路径,目录结构,IDE设置(如果您使用) - 取决于您计划如何使用SDK。

还有unittest / mock指令(我无法对它们发表评论 - 尚未使用它们。)

您可以尝试使用/ users / sadovnychi /示例中的'google'SDK子目录进行符号链接 - 但我无法确定这是否是您的设置的好建议。