如何修复PyQT6/PySide6在使用tox时抛出"qt.qpa.xcb:无法连接到显示器"的问题



我正试图使用pytest为PySide6应用程序编写测试。我也用毒物学进行这些测试;然而,我遇到了一个问题。在测试过程中,每次测试都失败,输出为:

tests/test_application.py::test_application_startup qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, minimalegl, wayland-egl, wayland, vkkhrdisplay, offscreen, linuxfb, vnc, minimal, xcb.
Fatal Python error: Aborted
<Stacktrace omitted due to SO believing it to be spam, sorry :(>
Extension modules: xxsubtype, shiboken6.Shiboken, PySide6.QtCore, PySide6.QtGui, PySide6.QtWidgets, PySide6.QtTest, PySide6.QtUiTools, markupsafe._speedups (total: 8)
Aborted (core dumped)

这是我的毒物配置:

[tox]
minversion = 3.24
envlist =
py310
isolated_build = True
[testenv]
description = Invoke pytest to run automated tests
setenv =
TOXINIDIR = {toxinidir}
passenv =
HOME
SETUPTOOLS_*
PYTHONPATH
deps =
PySide6==6.4.1
qt-material==2.12
setuptools
coverage
pytest
pytest-qt
commands =
coverage run --context={envname} -m pytest --strict-markers --strict-config --tb=short --verbose --capture=no --full-trace
parallel_show_output = True

我的环境:Ubuntu 20.04Python 3.10.8tox==3.27.1

失败测试代码:

from PySide6.QtWidgets import QApplication
def test_application_startup() -> None:
"""Ensure the application builds and configures."""
# Run the application
QApplication(sys.argv)

注意,我的环境中也安装了tox-conda,但是,我可以在没有它的情况下显示这个问题

由于我正在运行Miniconda,我曾考虑使用它而不是pip来安装PySide6,但没有成功。在终端的tox配置中运行命令会取得成功,所以我相信我的系统已经准备好了。这只是因为毒素而失败,而且只有毒素。

tox提供隔离,这也意味着环境变量不会自动传入。

我没有使用PyQT6的经验,所以我不知道具体的环境变量,但您可以尝试允许所有变量作为启动变量。

passenv = *

如果这样做有效,那么您可以阅读更多关于PyQT6的内容,并了解哪些环境变量是必要的。

最新更新