简体   繁体   English

使用 QT 运行 pytest 时发生致命 Python 错误

[英]Fatal Python Error when running pytest with QT

I'm new to pytest and I'm having trouble trying to run one of my test files.我是 pytest 的新手,我在尝试运行我的一个测试文件时遇到了麻烦。 I'm getting the following output:我得到以下 output:

================================================== 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)

The line it looks to be choking on is a call to "super" within the __init__() function of the CharacterInfowWindow class (subclass of QtWidgets.QMainWindow) within the character_info_window module.它看起来令人窒息的那一行是对 character_info_window 模块中 CharacterInfowWindow class(QtWidgets.QMainWindow 的子类)的 __init__() function 中的“super”的调用。 Any ideas on what could be the problem?关于可能是什么问题的任何想法?

I had this issue today as well.我今天也有这个问题。 The solution for me was to create a pytest.ini file in my tests directory and include this in it.我的解决方案是在我的测试目录中创建一个 pytest.ini 文件并将其包含在其中。

[pytest]
qt_api=pyqt5

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

To force a particular API, set the configuration variable qt_api in your pytest.ini file to pyqt5 or pyside2要强制使用特定的 API,请将 pytest.ini 文件中的配置变量 qt_api 设置为 pyqt5 或 pyside2

Once I set the qt_api in the pytest.ini the tests ran without any issues一旦我在 pytest.ini 中设置了 qt_api,测试就运行没有任何问题

I recognize this output, i had the same problem myself just recently我认识这个 output,我自己最近也遇到了同样的问题

The problem was that i had forgotten to create the QApplication before creating the widget/window i wanted to test问题是我在创建要测试的小部件/窗口之前忘记创建QApplication

So one way to solve the problem is this:所以解决问题的一种方法是:

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

Alternatively you can use a pytest fixture which takes care of setup/teardown或者,您可以使用负责安装/拆卸的 pytest 夹具

pytest-qt includes the qtbot fixture. pytest-qt包括qtbot固定装置。 From the project readme file:从项目自述文件中:

The main usage is to use the qtbot fixture, responsible for handling qApp creation as needed [...]主要用途是使用qtbot夹具,负责根据需要处理qApp创建 [...]

So in that case this is enough:所以在这种情况下,这就足够了:

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

In my case I ran into the Fatal Python error: aborted message, but it turned out to be due to my environment.在我的情况下,我遇到了Fatal Python error: aborted消息,但结果是由于我的环境。

I was seeing failures running the test suite in GitHub Codespaces but when I went the extra step of cloning it, the suite worked under Crostini on my Chromebook.我看到在 GitHub 代码空间中运行测试套件失败,但是当我进行克隆它的额外步骤时,该套件在我的 Chromebook 上的 Crostini 下运行。 Once I realized that it worked where I had a DISPLAY variable defined I realized that the Qt tests probably required a DISPLAY , whether xvfb or a "real" display, so I installed xvfb and then used xvfb-run pytest -v and was able to see the test suite succeed.一旦我意识到它在我定义了一个DISPLAY变量的地方工作,我意识到 Qt 测试可能需要一个DISPLAY ,无论是xvfb还是“真实”显示器,所以我安装了xvfb然后使用xvfb-run pytest -v并且能够看到测试套件成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM