Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create (python) QApplication in github action

I've got some unit tests for my (python) Qt gui, which require QApplication instance, but creating one always fail for me (i.e. ends in core dumped and application abort at line with QApplication()). What I've tried so far is:

  • creation methods:
    • plain app = QApplication() on module level
    • app = QApplication(['--platform offscreen'])
    • using fixture from pytest-qt that manages QApplication object creation, i.e. passing qtbot to my tests
  • I've even tried both python ports of qt, i.e.:
    • PyQt5
    • PySide2
  • Virtual screens:
    • I've tried running xvfb
    • I've also tried with window manager herbstluftwm on top of Xvfb
    • I've tried installing x11-utils libxkbcommon-x11-0 as suggested in QApplication instance/qtbot fixture causes travis-ci to abort and core dump

I've tried using https://github.com/nektos/act to debug this issue locally, but using this approach issue was not reproducible (i.e. everything worked as expected) until I've added herbstluftwm, i.e. only thing I was able to achieve is that locally it also started to fail.

What else I can check? Have you seen QApplication created successfully on github actions? BTW. How to get Qt's output to be visible in github actions? (I've added env: QT_DEBUG_PLUGINS: 1 and sill can't see any errors)

like image 957
konserw Avatar asked May 16 '26 13:05

konserw


2 Answers

After running into this issue again and again, I stitched a few things together and for PyQt6, this works now (without xvfb-run): https://github.com/GuckLab/MPL-Data-Cast/blob/main/.github/workflows/check.yml

runs-on: ${{ matrix.os }}
strategy:
  matrix:
    python-version: ['3.10']
    os: [macos-latest, ubuntu-latest, windows-latest]
env:
  # Display must be available globally for linux to know where xvfb is
  DISPLAY: ":99.0"
  QT_SELECT: "qt6"

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
  uses: actions/setup-python@v4
  with:
    python-version: ${{ matrix.python-version }}
- name: Setup xvfb (Linux)
  if: runner.os == 'Linux'
  run: |
    # Stuff copied wildly from several stackoverflow posts
    sudo apt-get install -y xvfb libxkbcommon-x11-0 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-xinerama0 libxcb-xinput0 libxcb-xfixes0 libxcb-shape0 libglib2.0-0 libgl1-mesa-dev
    sudo apt-get install '^libxcb.*-dev' libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev
    # start xvfb in the background
    sudo /usr/bin/Xvfb $DISPLAY -screen 0 1280x1024x24 &
- name: Install Python dependencies
  run: |
    # prerequisites
    python -m pip install --upgrade pip wheel
    python -m pip install coverage flake8 pytest pytest-qt
- name: Install package
  run: |
    pip install -e .
- name: List installed packages
  run: |
    pip freeze
- name: Test with pytest
  run: |
    coverage run --source=mpl_data_cast -m pytest -x tests
like image 64
Paul Avatar answered May 19 '26 02:05

Paul


Thanks to @eyllanesc request for MRE I've created this https://github.com/konserw/mre minimal example repo which allowed me to find solution on my own. It turns out that in you need to install xvfb and libxkbcommon-x11-0, but you must NOT run xvfb service or herbstluftwm. Then you need to run your test command (coverage in my case) using xvfb-run, which in case of github actions require absolute path to coverage, like that:

xvfb-run `which coverage` run -m pytest

I hope this would help future users of github actions struggling to get PyQt5 or PySide2 GUI tests running.

BTW. pytest was silencing output from Qt's QT_DEBUG_PLUGINS, so replacing test command with plain python call with some minimal script that reproduces problem was key here. see https://github.com/konserw/mre/runs/509156615?check_suite_focus=true

like image 32
konserw Avatar answered May 19 '26 01:05

konserw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!