运行 当测试位于兄弟文件夹中时,通过命令行进行单个测试

Run a single test via a command line when tests are located within a sibling folder

我有一个文件夹结构:

app/
|
|-src/
| |
| |-Code.py
|
|-tests/
  |
  |-__init__.py
  |-test_Code.py

我 运行 我通过发出命令进行测试:

app$ python3 -m unittest discover

因此,我在 test_Code.py 文件中的所有测试都是 运行。

我想 运行 来自 test_Code.py 文件的单个测试。

'test_Code.py'的内容是:

import unittest
from src.Code import Code

class TestCase1(unittest.TestCase):

    def test_Code1(self):
        ...

    def test_Code2(self):
        ...

if __name__ == '__main__':
    unittest.main()

有一个关于运行单测的问题:Running single test from unittest.TestCase via command line

给出的答案是:

python testMyCase.py MyCase.testItIsHot

在我的情况下(我认为):

app$ python3 tests/test_Code.py TestCase1.test_Code1

不幸的是,这会导致错误:

ImportError: No module named 'src'

有人评论说,如果测试位于子目录中,则提议的方法不起作用。提出决议:

python test/testMyCase.py test.MyCase.testItIsHot

因此我尝试:

app$ python3 tests/test_Code.py test_Code.TestCase1.test_Code1

这会导致相同的错误。

我也尝试过一些不同的方法:

app$ python3 -m unittest tests/test_Code.py test_Code.TestCase1.test_Code1

但这 运行 就是所有的测试。

那么问题是: 当测试位于兄弟文件夹中时,如何通过命令行 运行 进行单个测试?

这应该有效。

python -m unittest -v tests.test_Code.TestCase1.test_Code1

假设 src 也是一个模块