RuntimeError: could not open display

RuntimeError: could not open display

我有一个包含

的单元测试
import gtk

其中包含用于测试信号处理程序和 GUI 的单元测试 类。当我从 shell 运行 py.test 连接 X11 显示器时,这工作正常。但是,当我尝试 运行 tox 时,我得到:

______________________ ERROR collecting tests/test_gui.py ______________________
tests/test_gui.py:17: in <module>
    from rprs_bootstrap.gui import GUI
src/gui.py:12: in <module>
    import gtk.glade
.tox/py27/lib/python2.7/site-packages/gtk-2.0/gtk/__init__.py:64: in <module>
    _init()
.tox/py27/lib/python2.7/site-packages/gtk-2.0/gtk/__init__.py:52: in _init
    _gtk.init_check()
E   RuntimeError: could not open display
!!!!!!!!!!!!!!!!!!!! Interrupted: stopping after 1 failures !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.34 seconds ============================
make: *** [tox] Error 2

我可以从 tox.ini 中启动一个 Xvfb 服务器,但这似乎不够优雅。 有什么建议吗?

使用pytest-xvfb

哪个

[...] runs your testsuite with Xvfb to avoid popping up windows during GUI tests or allow them to run on systems without a display (like a CI).

要安装:

pip install pytest-xvfb