I have a script 'interceptor.py' with a function
def isIPValid(string):
and a unit test script for pytest 'test_interceptor.py' with a test for this function:
import interceptor
def test_isIPValid():
ip1 = 'localhost'
ip2 = '192.168.4.52'
ip3 = '55..5.7.1'
ip4 = 'badip'
assert interceptor.isIPValid(ip1)
assert interceptor.isIPValid(ip2)
assert not interceptor.isIPValid(ip3)
assert not interceptor.isIPValid(ip4)
Running the tests locally either from PyCharm or cmd works well
pytest
or
coverage run -m pytest
work and the test passes
On Gitlab CI however, when running coverage run -m pytest
(after cloning and installing pytest and coverage via pip) I get the following output:
> assert interceptor.isIPValid(ip1)
E AttributeError: module 'interceptor' has no attribute 'isIPValid'
test_interceptor.py:10: AttributeError
=========================== short test summary info ============================
FAILED test_interceptor.py::test_isIPValid - AttributeError: module 'interceptor' has no attribute 'isIPValid'
============================== 1 failed in 0.09s ===============================
I've tried importing the specific function or all of the functions from the module, always with the same error. Does anyone have a pointer where this issue could come from and why I am getting different behaviors on the Gitlab runner vs locally on Windows?
Some additional information:
- The pipeline is running on a linux runner in hosted Gitlab
- All requirements are installed from a requirements.txt file during the CI script, plus pytest and coverage
- The file containing the function uses PySide6, but the tested function does not use any function that depends on it.
Edit:
As requested, here is my gitlab yaml
stages:
- test
image: python:3
unit-test-job: # This job runs in the test stage.
stage: test
script:
- python --version
- pip install -r requirements.txt
- pip install coverage
- pip install pytest
- pip install pytest-mock
- echo "Running unit tests"
- coverage run -m pytest
- coverage report
- coverage xml
... plus the upload part we don't actually reach
Edit 2:
There seems to be a serious and unaddressed bug in PySide6 when using GitlabCI, as pointed out in this thread:
Why does PySide6 on GitLab CI result in ImportError?
This solution, however, is still not working for me:
This
script:
- python -m venv .venv
- . .venv/bin/activate
- pip install -r requirements.txt
- pip install coverage
- pip install pytest
- pip install pytest-mock
- python -m pip install PySide6
- strip --remove-section=.note.ABI-tag .venv/lib/python3.11/site-packages/PySide6/Qt/lib/libQt6Core.so.6
- python -c 'import PySide6; print(PySide6.__version__)'
- ldd .venv/lib/python3.11/site-packages/PySide6/Qt/lib/libQt6Core.so.6
- python -c 'import PySide6.QtCore'
- echo "Running unit tests"
- coverage run -m pytest
- coverage report
- coverage xml
Still generates an error:
ImportError while importing test module '<mypath>test_interceptor.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.11/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
test_interceptor.py:1: in <module>
from Interceptor_module import isIPValid
Interceptor_module.py:5: in <module>
from PySide6 import QtCore, QtGui, QtWidgets
E ImportError: libGL.so.1: cannot open shared object file: No such file or directory
=========================== short test summary info ============================
ERROR test_interceptor.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.86s ===============================