2016-06-15 2 views
0

複数のファイルにまたがってnosetestsを実行していて、特定のファイルのインポートに関するエラーが発生しました。それは、インポートとは何か、またはそれにパッチを当てて何かであると考えてください。Noestests:Nosetestsを複数のファイルで実行しているときに@patchを使用しているAttributeError

my_package 
    - my_module 
     - __init__.py 
     - utils.py 
     - other.py 
    - tests 
     - test_utils.py 
     - test_other.py 
:パッケージ構造は次のようになります

(私は@patchデコレータを使用する各テスト機能のためにこれらのいずれかのエラーを取得しています)

Error 
Traceback (most recent call last): 
    File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/unittest2/case.py", line 67, in testPartExecutor 
yield 
    File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/unittest2/case.py", line 625, in run 
testMethod() 
    File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1297, in patched 
arg = patching.__enter__() 
    File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1353, in __enter__ 
self.target = self.getter() 
    File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1523, in <lambda> 
getter = lambda: _importer(target) 
    File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1210, in _importer 
thing = _dot_lookup(thing, comp, import_path) 
    File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1200, in _dot_lookup 
return getattr(thing, comp) 
AttributeError: 'module' object has no attribute 'utils' 

:ようなエラー自体が見えます

nosetestsは、コマンド:

nosetests -e unit --with-coverage --cover-package=my_package --cover-erase --cover-xml --with-xunit tests --nocapture

奇妙なことは、utilsテストクラス自体でのみnosetestを実行すると、すべてのインポート作業とすべてのパッチが動作し、エラーは発生せず、すべてのテストが合格します。

from my_module.other import * 

class TestBusinessProcess(unittest2.TestCase):   

    @patch('my_module.other.something') 
    def test_some_function(self, something_mock): 
     # test implementation.. 
     # no issues! 
     pass 

    @patch('my_module.other.something_else') 
    def test_some_other_function(self, something_else_mock): 
     # test implementation.. 
     # no issues! 
     pass 

任意の助けを大いに:

from my_module.utils import * 

class TestBusinessProcess(unittest2.TestCase):   

    @patch('my_module.utils.something') 
    def test_some_utils_function(self, something_mock): 
     # test implementation.. 
     # this function will throw: 
     # AttributeError: 'module' object has no attribute 'utils' 
     # when running whole tests folder and not on individual test file 
     pass 

    @patch('my_module.utils.something_else') 
    def test_some_other_utils_function(self, something_else_mock): 
     # test implementation.. 
     # same as above 
     pass 

いずれかの方法を実行しました何の問題を持っていない他のテストファイル内のテストの例:ここでは

はtest_utils.pyファイルは次のようになります。感謝。

答えて

0

私はまだ何が間違っているのか分かりませんが、インポートとは何か関係があるようでした。

とにかく、この回避策は問題を解決しましたが、その理由を正確にはわかりません。

my_module__init__.pyは最初は空でした。それから私はutils.pyの個々の機能を公開し、それを編集した:

__init__.py

from utils import test_some_utils_function, test_some_other_utils_function 

__all__ = [ 
    "test_some_utils_function", 
    "test_some_other_utils_function" 
]   
関連する問題