我正在为我的 Python 包开发 Jenkins CI/CD 管道。我的项目文件层次结构如下:
project/
- package_name
- file1.py
- file2.py
- etc...
- tests
- unit
- __main__.py
- __init__.py
- test1.py
- test2.py
所有单元测试(我正在使用unittest
)使用单个命令运行
python -m tests.unit
通过添加__init__.py
包含以下内容:
contents
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
and __main__.py
看起来像这样
contents
import unittest
import sys
sys.path.append('../..')
loader = unittest.TestLoader()
start_dir = '.'
suite = loader.discover(start_dir)
runner = unittest.TextTestRunner(verbosity=2).run(suite)
首先将路径改为./tests/unit
。之后,将顶级目录添加到导入路径中,以便在测试中导入该包。这按预期工作(即,所有休息都通过运行来执行python -m test.unit
在我的个人笔记本电脑(Python 3.6.4)上的项目目录顶部)。
但是,当我在远程 Jenkins 服务器(以及 Python 3.6.4)上使用相同的技巧时,我收到以下错误:
no module named test.unit.__main__; 'test.unit' is a package and cannot be directly executed
我已经研究了这个问题,但所提出的解决方案似乎都不适用于我的情况。
如何修改我的代码以创建测试套件unittest
本地和远程运行不会有任何问题吗?
EDIT我尝试修改PYTHONPATH
变量,但没有成功