python包出错
我知道我的问题是多余的,在互联网上有很多答案,但我已经尝试了每一个建议的解决方案,我仍然无法设法得到它的权利 我试图将我的单元测试与python源代码分离 因此,我的目录如下所示:python包出错,python,python-3.x,package,python-unittest,Python,Python 3.x,Package,Python Unittest,我知道我的问题是多余的,在互联网上有很多答案,但我已经尝试了每一个建议的解决方案,我仍然无法设法得到它的权利 我试图将我的单元测试与python源代码分离 因此,我的目录如下所示: HelloWorld __init__.py source __init__.py helloworld.py test __init__.py test_helloworld.py class HelloWorld:
HelloWorld
__init__.py
source
__init__.py
helloworld.py
test
__init__.py
test_helloworld.py
class HelloWorld:
def __init__(self):
self.message = 'Hello, World!'
import unittest
from source.helloworld import HelloWorld
class MyTestCase(unittest.TestCase):
def test_message(self):
hw = HelloWorld()
self.assertEqual(hw.message, 'Hello, World!')
if __name__ == '__main__':
unittest.main()
我的源代码/helloworld.py如下所示:
HelloWorld
__init__.py
source
__init__.py
helloworld.py
test
__init__.py
test_helloworld.py
class HelloWorld:
def __init__(self):
self.message = 'Hello, World!'
import unittest
from source.helloworld import HelloWorld
class MyTestCase(unittest.TestCase):
def test_message(self):
hw = HelloWorld()
self.assertEqual(hw.message, 'Hello, World!')
if __name__ == '__main__':
unittest.main()
我的test/test_helloworld.py如下所示:
HelloWorld
__init__.py
source
__init__.py
helloworld.py
test
__init__.py
test_helloworld.py
class HelloWorld:
def __init__(self):
self.message = 'Hello, World!'
import unittest
from source.helloworld import HelloWorld
class MyTestCase(unittest.TestCase):
def test_message(self):
hw = HelloWorld()
self.assertEqual(hw.message, 'Hello, World!')
if __name__ == '__main__':
unittest.main()
现在,当我在PyCharm中运行我的测试时,在将两个目录(源和测试)标记为Sources root之后,我的测试运行并说OK(代码通过了测试)
但是,当我尝试从终端运行测试时,会得到错误ModuleNotFoundError:没有名为“source”的模块
我就是看不懂这个问题。我需要修改PYTHONPATH吗?这不是init.py文件应该做的吗 通过调用
test/
内部的脚本,除非您做更多的工作,否则实际上会使它外部的一切都不可见。相反,移动到HelloWorld/
并调用python-m test.test\u HelloWorld
如何“从终端”运行它?python test\u HelloWorld.pythanker@Ignacio!它确实有效!但是你能解释一下-m是做什么的吗?如果我试图从test/内部调用脚本,唯一的方法就是将目录附加到路径?