I am working on a Jenkins CI/CD pipeline for my Python package. My project file hierarchy is as follows:
project/
- package_name
- file1.py
- file2.py
- etc...
- tests
- unit
- __main__.py
- __init__.py
- test1.py
- test2.py
All unit tests (I am using unittest
) are run using a single command
python -m tests.unit
via adding __init__.py
of the following content:
contents
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
and __main__.py
which looks like this
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)
First, the path is changed to the ./tests/unit
. After that, the top directory is added to the import path so that the package can be imported in tests. This works as intended (i.e., all rests are executed by running python -m test.unit
at the top of the project directory) on my personal laptop (Python 3.6.4).
However, when I use the same trick on a remote Jenkins server (Python 3.6.4 as well), I am getting the following error:
no module named test.unit.__main__; 'test.unit' is a package and cannot be directly executed
I have researched the issue but none of the proposed solutions seems to be working in my case.
How can I modify my code to create a test suite in unittest
that will run without any issues locally and remotely?
EDIT
I tried modifying the PYTHONPATH
variable, but no success