关于python:Setuptools – 当测试不在主目录中时如何运行unittest测试套件?

Setuptools - how to run unittest test suite when tests are not in main directory?

我有以下模块结构:

1
2
3
4
5
6
main
->module
--->tests
----->test_module.py
--->module.py
->setup.py

我使用unittest包实现了单元测试。 按照这个答案时,我将setup.py文件设置为:

1
2
3
4
5
6
7
from setuptools import setup

setup(
    name='module',
    packages=['module', ],
    test_suite='module/tests',
)

生成以下错误消息:

====================================================================== ERROR: tests (unittest.loader._FailedTest)
---------------------------------------------------------------------- ImportError: Failed to import test module: tests Traceback (most
recent call last): File
"/opt/conda/lib/python3.6/unittest/loader.py", line 153, in
loadTestsFromName
module = import(module_name) ModuleNotFoundError: No module named 'tests/module'

Ran 1 test in 0.000s

FAILED (errors=1) Test failed: error: Test failed:

设置test_suite='tests时,我得到了:

====================================================================== ERROR: tests (unittest.loader._FailedTest)
---------------------------------------------------------------------- ImportError: Failed to import test module: tests Traceback (most
recent call last): File
"/opt/conda/lib/python3.6/unittest/loader.py", line 153, in
loadTestsFromName
module = import(module_name) ModuleNotFoundError: No module named 'tests'

Ran 1 test in 0.000s

FAILED (errors=1) Test failed: error: Test failed:

有人可以帮我弄这个吗?


test_suite是测试包的名称(用Python点缀语法),而不是目录路径,因此语法必须是

1
test_suite='module.tests',