【发布时间】:2018-10-14 01:25:52
【问题描述】:
我的目录结构如下:
.
├── README.md
├── src
│ ├── __init__.py
│ └── foo.py
└── test
├── __init__.py
└── runner.py
└── test_foo.py
测试文件如下所示:
test_foo.py
import unittest
from ..src.foo import *
class TestFoo(unittest.TestCase):
def setUp(self):
pass
def test_foo(self):
foo = Foo()
res = foo.get_something('bar')
if res is None:
self.fail('something bad happened')
if __name__ == '__main__':
unittest.main(self)
runner.py
import unittest
# import test modules
import test_foo
# initialize the test suite
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# add tests to the test suite
suite.addTests(loader.loadTestsFromModule(test_foo))
# initialize a runner, pass it your suite and run it
runner = unittest.TextTestRunner(verbosity=3)
result = runner.run(suite)
我想在测试套件的父目录中运行我的所有测试,所以我尝试像这样调用 unites:
python -m test/runner.py
但它抱怨以下内容:
$ python -m test/runner.py
/usr/bin/python: Import by filename is not supported.
如果我移动到测试目录,我会得到一个不同的错误:
$ python -m runner
File "test_foo.py", line 2, in <module>
from ..src.foo import *
ValueError: Attempted relative import in non-package
如果可能的话,我想将所有与测试相关的东西保留在父/测试目录中。
知道我在这里做错了什么吗?
谢谢!
【问题讨论】:
-
我强烈推荐使用
pytest而不是unittest -
为什么你会推荐 pytest 而不是 unitest?
-
尝试使用
python -m test.runner运行 -
我现在远离我的代码,但我相信我试过了,但它抱怨找不到名为“runner”的模块
标签: python python-unittest test-suite