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

Posted

技术标签:

【中文标题】使用 QT 运行 pytest 时出现致命的 Python 错误【英文标题】:Fatal Python Error when running pytest with QT 【发布时间】:2020-05-06 13:30:25 【问题描述】:

我是 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”的调用。关于可能是什么问题的任何想法?

【问题讨论】:

请提供minimal reproducible example 外部参照 ***.com/questions/64959516/…> 【参考方案1】:

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

[pytest]
qt_api=pyqt5

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

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

一旦我在 pytest.ini 中设置了 qt_api,测试就可以毫无问题地运行

【讨论】:

【参考方案2】:

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

问题是我在创建要测试的小部件/窗口之前忘记创建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()
    [...]

【讨论】:

【参考方案3】:

就我而言,我遇到了Fatal Python error: aborted 消息,但结果是我的环境造成的。

我看到在 GitHub Codespaces 中运行测试套件失败,但是当我执行克隆它的额外步骤时,该套件在我的 Chromebook 上的 Crostini 下运行。一旦我意识到它在我定义了一个DISPLAY 变量的地方工作,我意识到Qt 测试可能需要DISPLAY,无论是xvfb 还是“真实”显示,所以我安装了xvfb,然后使用@987654326 @ 并且能够看到测试套件成功。

【讨论】:

以上是关于使用 QT 运行 pytest 时出现致命的 Python 错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 pytest 运行测试时出现问题 - 在我自己的包中找不到模块

尝试运行可执行 Python 脚本时出现致命的 Python 错误

在 Windows 中编译 Qt 时出现致命错误

运行 py.test 时出现错误 ImportMismatchError

尝试获取具有自定义类型的 DBus 属性时出现致命错误

使用 xampp 运行本地项目时出现致命错误