2

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

4

3 回答 3

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,测试就会运行没有任何问题

于 2021-02-17T16:41:27.560 回答
0

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

问题是我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()
    [...]
于 2021-02-16T16:28:13.990 回答
0

In my case I ran into the Fatal Python error: aborted message, but it turned out to be due to my environment.

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. 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.

于 2021-08-20T16:23:41.080 回答