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

Imports are not properly handled while running nosetests in multi process mode

给定一个测试用例:

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']

知道它为什么会发生以及如何解决吗?

AttributeError: 'module' object has no attribute 'appengine'

这表明 GAE SDK 的位置不正确。

检查您的 SDK installation/usage 说明、python 路径、目录结构、IDE 设置(如果您使用的话)- 取决于您打算如何使用 SDK。

还有 unittest/mock 说明(我无法对它们发表评论 - 还没有使用过)。

例如,您可以尝试对 /Users/sadovnychi/example 中的 'google' SDK 子目录进行符号链接 - 但我无法判断这是否适合您的设置。

所以我在 nosegae.py 中漏掉了一行:

del sys.modules['google']

已经固定到 reload(sys.modules['google'])

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

问题解决了。