BUG: fatal error when running pytest-qt on tox or container
Description
When running the test environment with tox or inside a docker container, a fatal error occurs when pytest tries to run any test that depends on qtbot fixture.
Related to these questions on stackoverflow:
- https://stackoverflow.com/questions/77085350/pyqtest-qt-fail-on-github-action
- https://stackoverflow.com/questions/60692711/cant-create-python-qapplication-in-github-action
tox test environment output
test: commands[3]> coverage run -m pytest
============================= test session starts ==============================
platform linux -- Python 3.11.9, pytest-8.3.2, pluggy-1.5.0
cachedir: .tox/test/.pytest_cache
PyQt5 5.15.11 -- Qt runtime 5.15.14 -- Qt compiled 5.15.14
rootdir: /home/brenopelegrin/ciermag/pqwidget
configfile: pytest.ini
plugins: cov-5.0.0, qt-4.4.0, pylama-8.4.1
collected 48 items
tests/backend/test_pint_interface.py ............. [ 27%]
tests/backend/test_utils.py . [ 29%]
tests/test_defaults.py .. [ 33%]
tests/validators/test_scientificcapturedgroups.py .. [ 37%]
tests/validators/test_scientificinputvalidator.py ...... [ 50%]
Fatal Python error: Aborted
Current thread 0x000079f4a1759440 (most recent call first):
File "/home/brenopelegrin/ciermag/pqwidget/tests/conftest.py", line 17 in app
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/fixtures.py", line 897 in call_fixture_func
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/fixtures.py", line 1139 in pytest_fixture_setup
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_callers.py", line 103 in _multicall
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_manager.py", line 120 in _hookexec
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_hooks.py", line 513 in __call__
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/fixtures.py", line 1090 in execute
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/fixtures.py", line 616 in _get_active_fixturedef
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/fixtures.py", line 531 in getfixturevalue
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/fixtures.py", line 696 in _fillfixtures
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/python.py", line 1630 in setup
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/runner.py", line 514 in setup
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/runner.py", line 160 in pytest_runtest_setup
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_callers.py", line 103 in _multicall
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_manager.py", line 120 in _hookexec
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_hooks.py", line 513 in __call__
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/runner.py", line 242 in <lambda>
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/runner.py", line 341 in from_call
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/runner.py", line 241 in call_and_report
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/runner.py", line 126 in runtestprotocol
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/runner.py", line 113 in pytest_runtest_protocol
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_callers.py", line 103 in _multicall
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_manager.py", line 120 in _hookexec
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_hooks.py", line 513 in __call__
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/main.py", line 362 in pytest_runtestloop
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_callers.py", line 103 in _multicall
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_manager.py", line 120 in _hookexec
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_hooks.py", line 513 in __call__
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/main.py", line 337 in _main
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/main.py", line 283 in wrap_session
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/main.py", line 330 in pytest_cmdline_main
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_callers.py", line 103 in _multicall
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_manager.py", line 120 in _hookexec
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pluggy/_hooks.py", line 513 in __call__
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/config/__init__.py", line 175 in main
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/_pytest/config/__init__.py", line 201 in console_main
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/pytest/__main__.py", line 9 in <module>
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/coverage/execfile.py", line 211 in run
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/coverage/cmdline.py", line 858 in do_run
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/coverage/cmdline.py", line 681 in command_line
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/lib/python3.11/site-packages/coverage/cmdline.py", line 970 in main
File "/home/brenopelegrin/ciermag/pqwidget/.tox/test/bin/coverage", line 8 in <module>
Extension modules: PyQt5.QtCore, PyQt5.QtGui, PyQt5.QtWidgets, PyQt5.QtTest (total: 4)
tests/widget/gui/test_dimensionless_with_thousand_sep.py test: exit -6 (4.93 seconds) /home/brenopelegrin/ciermag/pqwidget> coverage run -m pytest pid=33742
test: FAIL code -6 (10.98=setup[4.13]+cmd[0.69,1.00,0.23,4.93] seconds)
evaluation failed :( (11.52 seconds)
Solution
The problem is that Qt needs a display to run, so we need to create a virtual display using xvfb-run.
On xvfb-run manual, it says:
xvfb-run is a wrapper for the Xvfb(1x) command which simplifies the task of running commands (typically an X client, or a script containing a list of clients to be run) within a virtual X server environment.
So, to fix the problem, we just need to run pytest with xvfb-run:
xvfb-run coverage run -m pytest