我想写使用试用一些单元测试我扭曲的应用程序, 我写单元测试类我的第一个“空”审判,但是当我尝试运行它,我得到导入错误导入我的应用程序模块。扭曲试验单元测试得到的ImportError
这是因为我怀疑因为试验更改当前的工作目录 当我尝试导入我的模块与对象类,我想单元测试 它失败。
我将我的应用程序模块保存在我自己设置的单个目录中,它不在PYTHONPATH的任何目录中,或者在我的应用程序中,模块导入其他模块,因为它们都位于相同目录中。
代码看起来类同这样的:
from twisted.trial import unittest
from twisted.spread import pb
from twisted.internet import reactor, protocol
from MyModule import MyTestSubject
class MyTestSubjectTest(unittest.TestCase):
def setUp(self):
print('\nset up')
def test_startConsoleServer(self):
ts = MyTestSubject()
.... # here goes the body of the test
def tearDown(self):
print('\ntear down')
所以错误味精是这样的:
exceptions.ImportError:无模块名为MyModule的
也许这不是标准使用试用或部署python应用程序的方式。
更新:我只是想出了解决类似的问题,只是追加app目录到sys.path中 这样的进口部分将是这样的:
from twisted.trial import unittest
from twisted.spread import pb
from twisted.internet import reactor, protocol
import sys, os; sys.path.append(os.path.abspath(os.path.curdir))
from MyModule import MyTestSubject
什么是确切的导入错误?什么是不被导入? – 2011-04-27 19:37:06
对不起,忘了提。 MyModule中没有找到“exceptions.ImportError:无模块名为MyModule的”,我刚刚找到了一个解决办法,我加入,我保持我的应用程序移到sys.path – mik80 2011-04-27 19:53:45