使用 QT 运行 pytest 时出现致命 Python 错误

问题描述 投票:0回答:4

我是 pytest 新手,在尝试运行我的测试文件之一时遇到问题。我得到以下输出:

================================================== test session starts ==================================================
platform linux -- Python 3.7.6, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- /home/jarel/anaconda3/bin/python
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/home/jarel/workspace/spyder/rpgland/.hypothesis/examples')
PyQt5 5.9.2 -- Qt runtime 5.9.7 -- Qt compiled 5.9.6
rootdir: /home/jarel/workspace/spyder/rpgland
plugins: astropy-header-0.1.2, cov-2.8.1, openfiles-0.4.0, arraydiff-0.3, hypothesis-5.5.4, qt-3.3.0, doctestplus-0.5.0, remotedata-0.3.2
collected 1 item                                                                                                        

rpgland/test_character_window.py::test_init_character Fatal Python error: Aborted

Current thread 0x00007fb5b7229740 (most recent call first):
  File "/home/jarel/workspace/spyder/rpgland/rpgland/view/character_info_window.py", line 9 in __init__
  File "/home/jarel/workspace/spyder/rpgland/rpgland/test_character_window.py", line 20 in gui
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/fixtures.py", line 788 in call_fixture_func
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/fixtures.py", line 958 in pytest_fixture_setup
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 87 in <lambda>
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 93 in _hookexec
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/fixtures.py", line 909 in execute
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/fixtures.py", line 580 in _compute_fixture_value
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/fixtures.py", line 499 in _get_active_fixturedef
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/fixtures.py", line 483 in getfixturevalue
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/fixtures.py", line 473 in _fillfixtures
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/fixtures.py", line 294 in fillfixtures
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/python.py", line 1449 in setup
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/runner.py", line 370 in prepare
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/runner.py", line 122 in pytest_runtest_setup
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 87 in <lambda>
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 93 in _hookexec
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/runner.py", line 210 in <lambda>
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/runner.py", line 237 in from_call
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/runner.py", line 210 in call_runtest_hook
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/runner.py", line 185 in call_and_report
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/runner.py", line 93 in runtestprotocol
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/runner.py", line 84 in pytest_runtest_protocol
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 87 in <lambda>
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 93 in _hookexec
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/main.py", line 271 in pytest_runtestloop
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 87 in <lambda>
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 93 in _hookexec
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/main.py", line 247 in _main
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/main.py", line 197 in wrap_session
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/main.py", line 240 in pytest_cmdline_main
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 87 in <lambda>
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/manager.py", line 93 in _hookexec
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__
  File "/home/jarel/anaconda3/lib/python3.7/site-packages/_pytest/config/__init__.py", line 93 in main
  File "/home/jarel/anaconda3/bin/pytest", line 11 in <module>
Aborted (core dumped)

它看起来令人窒息的行是对character_info_window模块内CharacterInfowWindow类(QtWidgets.QMainWindow的子类)的__init__()函数中的“super”的调用。关于可能出现什么问题有什么想法吗?

python python-3.x pyqt pytest pytest-qt
4个回答
2
投票

我今天也遇到这个问题了。我的解决方案是在我的测试目录中创建一个 pytest.ini 文件并将其包含在其中。

[pytest]
qt_api=pyqt5

来自 https://github.com/pytest-dev/pytest-qt

要强制使用特定 API,请将 pytest.ini 文件中的配置变量 qt_api 设置为 pyqt5 或 pyside2

一旦我在 pytest.ini 中设置了 qt_api,测试就可以正常运行


1
投票

我认识这个输出,我最近也遇到了同样的问题

问题是我在创建我想要测试的小部件/窗口之前忘记创建

QApplication

解决问题的一种方法是:

def test_something():
    test_app = QtWidgets.QApplication(sys.argv)  # <-----
    main_win = some_application.main_window.MainWindow()
    [...]

或者,您可以使用 pytest 固定装置来处理设置/拆卸

pytest-qt 包含 qtbot 夹具。来自项目自述文件:

主要用法是使用

qtbot
夹具,负责根据需要处理
qApp
创建 [...]

所以在这种情况下这就足够了:

def test_something(qtbot):  # <-----
    main_win = some_application.main_window.MainWindow()
    [...]

0
投票

就我而言,我遇到了

Fatal Python error: aborted
消息,但事实证明这是由于我的环境所致。

我发现在 GitHub Codespaces 中运行测试套件失败,但当我采取额外步骤克隆它时,该套件在我的 Chromebook 上的 Crostini 下运行。一旦我意识到它在我定义了

DISPLAY
变量的地方工作,我意识到 Qt 测试可能需要
DISPLAY
,无论是
xvfb
还是“真实”显示,所以我安装了
xvfb
然后使用
xvfb-run pytest -v 
并能够看到测试套件成功。


0
投票

我曾经在 Visual Studio Code 单元测试插件中使用 QT 运行 pytest 时得到信号:SIGABRT。

如果 QT 代码没有主窗口,您会收到该信号。

只需将其添加到您的代码中,它就会为您创建一个:

from pyvirtualdisplay import Display

@pytest.fixture(scope="session", autouse=True)
def virtual_display():
    # Start a virtual display
    display = Display(visible=0, size=(800, 600))
    display.start()

    # Yield to allow tests to run inside the virtual display context
    yield

    # No need to Stop the virtual display after all tests are done
    # The test framework runner (vscode) does that automatically

但是,在测试内置 GUI 信号(如按钮按下、字段更新等)时,您仍然会受到限制。那是另一个话题了。

© www.soinside.com 2019 - 2024. All rights reserved.