假设我有以下文件夹结构,用于组织我的项目回购中的测试:
project/
app.py
tests/
test.py
app.py
具有简单的加法功能:
def add(x, y):
return x + y
test.py
测试此功能:
from app import add
import unittest
class TestCase(unittest.TestCase):
def test_add(self):
print('Running the test!')
self.assertEqual(add(3, 4), 7)
if __name__ == '__main__':
unittest.main()
当我使用
nose2
一切正常:
mac:project $ nose2
Running the test!
Ran 1 test in 0.000s
Ok
问题是当我试着跑步时
测验py公司
我直接得到以下错误:
mac:project $ python tests/test.py
ModuleNotFound: No module named 'app'
我怎样才能解决这个问题?原因是我想使用
coverage
包来衡量代码覆盖率,我得到了相同的错误:
mac:project $ coverage run tests/test.py
ModuleNotFound: No module named 'app'
我试过了
from ..app import app
但我得到了这个错误:
ValueError: attempted relative import beyond top-level package