我正在努力让导入为我的python项目工作。我已经创建了一个测试项目来说明我的问题。这是Python 3的版本。
我的目录结构如下:
project/
test.sh
packageA/
__init__.py
moduleA.py
test/
__init__.py
test_moduleA.py
test.sh的内容是python3 packageA/test/test_moduleA.py
两个__init__py
都为空。
这是moduleA.py
class A:
def doSomething(self):
print("Did something")
这是'test_moduleA.py`:
import unittest
from packageA.moduleA import A
class TestModuleA(unittest.TestCase):
def testSomething(self):
a = A()
self.assertTrue(a.doSomething() == "Did Something")
if __name__ == '__main__':
unittest.main()
当我运行test.sh
时,我得到的错误是:
[project] $ ./test.sh
Traceback (most recent call last):
File "packageA/test/test_moduleA.py", line 2, in <module>
from packageA.moduleA import A
ModuleNotFoundError: No module named 'packageA'
[project] $
我已经尝试在`test_moduleA.py中使用相对导入,如下所示:
from ..moduleA import A
在这种情况下,我得到的错误如下所示:
[project] $ ./test.sh
Traceback (most recent call last):
File "packageA/test/test_moduleA.py", line 2, in <module>
from ..moduleA import A
ValueError: attempted relative import beyond top-level package
[project] $
我怎样才能让它正常工作?Pythonic式的方法是什么?
发布于 2018-06-18 04:55:40
这是我推荐的基于this answer (另请参阅here)和this article的解决方案。
首先,将test目录移到项目的根目录中:
project/
test.sh
packageA/
__init__.py
moduleA.py
test/
__init__.py
test_moduleA.py
然后,按如下所示更改test_moduleA.py
(请注意导入):
import unittest
from packageA.moduleA import A
class TestModuleA(unittest.TestCase):
def testSomething(self):
a = A()
self.assertTrue(a.do_something() == "Something was done")
test.sh
是这样的:
#!/bin/bash
TEST_PACKAGE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${TEST_PACKAGE}
python3 -m unittest test.test_moduleA
为了通过测试,我还更改了a类:
class A:
def do_something(self):
print("do_somwthing was called")
return "Something was done"
现在,运行test.sh
,它应该会像预期的那样工作:
$ ./test.sh
do_somwthing was called
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
发布于 2018-06-18 04:12:29
使用外壳脚本来测试/部署python并不是一个好主意,所以我建议您用test.py
替换test.sh,它的内容是:
import unittest
testmodules = [
'packageA.test.test_moduleA',
]
suite = unittest.TestSuite()
for t in testmodules:
try:
mod = __import__(t, globals(), locals(), ['suite'])
suitefn = getattr(mod, 'suite')
suite.addTest(suitefn())
except (ImportError, AttributeError):
suite.addTest(unittest.defaultTestLoader.loadTestsFromName(t))
unittest.TextTestRunner().run(suite)
向其中添加新测试将非常简单,您只需修改testmodules列表即可。
https://stackoverflow.com/questions/50900030
复制相似问题