【问题标题】:Pytest segmentation fault and test failPytest 分段错误和测试失败
【发布时间】:2015-11-30 17:00:42
【问题描述】:

我正在尝试在我的 PyCharm project 上运行 pytest 测试。测试代码如下:

def test_window_created(self, create_xld_main_window):
    """This test tests, whether or not the Gtk.Window has been created.
    """
    screen = Wnck.Screen.get_default()
    screen.force_update()  # recommended per Wnck documentation
    window_list = screen.get_windows()

    for window in window_list:
        print(window.get_name())
        if window.has_name():
            if window.get_name() == self.xld_main_window.get_title():
                window_found = True
                break
    assert window_found, 'The Gtk.Window named {window_name} has not been found.'\
        .format(window_name=self.xld_main_window.get_title())

    # clean up Wnck (saves resources, check documentation)
    del window
    del screen
    Wnck.shutdown()

提到的夹具看起来像这样:

@pytest.fixture()
def create_xld_main_window(self):
    AppSettings.load_settings()
    VocableManager.load_vocables()
    self.xld_main_window = XLDMainWindow()
    self.xld_main_window.show_all()
    GTKGUITestHelper.refresh_gui()

refresh_gui 方法如下:

@classmethod
def refresh_gui(cls, delay=0):
    # print('delay', delay)
    while Gtk.events_pending():
        Gtk.main_iteration_do(blocking=False)
    time.sleep(float(delay))

当我按如下方式运行此测试时:

~/development/pycharm-workspace/gtkplus-tool/gtkplustool$ PYTHONPATH=~/development/pycharm-workspace/gtkplus-tool/ python -m pytest -v ~/development/pycharm-workspace/gtkplus-tool/test/

(我将项目根目录添加到 python 路径,以模拟在 PyCharm 中使用测试运行器时存在的相同条件。我读到 PyCharm 总是将项目根目录添加到 pythonpath。)

...我的测试得到以下输出:

================================================= test session starts ==================================================
platform linux -- Python 3.4.3 -- py-1.4.30 -- pytest-2.7.2 -- /home/xiaolong/development/anaconda3/envs/gtkplus-tool/bin/python
rootdir: /home/xiaolong/development/pycharm-workspace/gtkplus-tool/test, inifile: 
collected 6 items 

../test/example/example_gui_unit_test.py::MyViewTest::test_count PASSED
../test/example/example_gui_unit_test.py::MyViewTest::test_label PASSED
../test/gui/test_XLDMainWindow.py::TestXLDMainWindow::test_window_created Segmentation fault (core dumped)

此分段错误的原因可能是什么?

当我使用 PyCharm 测试运行程序运行此测试时,我得到以下结果:

  • 在正在运行或已执行的测试列表中,测试标有失败图标。
  • 终端输出并不表示测试失败。
  • 进度条是绿色的,好像所有测试都没有失败。
  • 统计显示:Test: test_XLDMainWindow.py Time elapsed: <TERMINATED>

更重要的是,由于分段错误,pytest 停止运行任何进一步的测试,所以我无法以舒适的方式运行其他测试。

【问题讨论】:

    标签: python unit-testing segmentation-fault pycharm pytest


    【解决方案1】:

    我现在已经弄清楚了,但我对 seg 错误的原因还很不满意。

    我评论了这一行:

    Wnck.shutdown()
    

    段错误消失了。但是,我现在不确定是否有任何泄漏或什么,当我没有正确关闭 Wnck 时。此外,此关闭以前也有效。几天前我运行了相同的代码。也许某些 Ubuntu 更新破坏了 Wnck 中的某些内容。

    欢迎任何进一步回答为什么这会导致段错误。

    【讨论】:

      猜你喜欢
      • 2016-11-02
      • 2016-06-02
      • 1970-01-01
      • 2019-09-14
      • 2018-04-05
      • 2021-02-06
      • 2015-02-03
      • 2017-03-07
      • 1970-01-01
      相关资源
      最近更新 更多