我正在使用 Python 3.6 和最新版本的 vscode。
我的文件夹结构:
/folder
/src
src.py
/tests
src_test.py
src.py:
class Addition:
def __init__(self, a, b):
self.a = a
self.b = b
def run(self):
return self.a + self.b
src_test.py:
import unittest
from ..src.addition import Addition
class TestAddition(unittest.TestCase):
def test_addition(self):
inst = Addition(5, 10)
res = inst.run()
self.assertEqual(res, 16)
if( __name__ == "main"):
unittest.main()
这是我的项目settings.json:
{
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"*_test.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
}
然后在项目根文件夹下:
python3 -m unittest 测试/src_test.py
File "/usr/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'tests.src_test'