0

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 ===============================
  • not enough info, we have to see your gitlab pipeline setup to help you. Can you add your `.gitlab-ci.yml` file as well? – Roman Pavelka Feb 09 '23 at 14:39
  • can you show the structure of the folders as well? – Tony Feb 09 '23 at 14:54
  • both the test script and the tested module are at root level – P Fernandez Feb 09 '23 at 14:59
  • can you try replacing the import with `import .interceptor` (to make it relative import), or create empty `__init__.py` file in the same folder and try again – Tony Feb 09 '23 at 15:12
  • now you are getting different error `ImportError: libGL.so.1: cannot open shared object file: No such file or directory` check if the file is there – Tony Feb 09 '23 at 15:20
  • I'm not sure where this file is meant to be located. I have listed the contents of Qt/lib and there is nothing by that name. It would seem some sort of OpenGL dependency. The output of the ldd command shows the following (not sure if related): `libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f8ab2f3d000)` – P Fernandez Feb 09 '23 at 15:31
  • Having this error means your package is tightly coupled and probably needs a refactoring as you should not need `libGL` to test this function, please read up on things like dependency inversion and software decoupling. There isn't more SO can do without more context. – ljmc Feb 09 '23 at 19:51

0 Answers0