Pytest分段故障和测试失败

时间:2015-09-04 13:45:33

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

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

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总是将项目根添加到python路径。)

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

================================================= 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停止运行任何进一步的测试,因此我无法以舒适的方式运行其他测试。

1 个答案:

答案 0 :(得分:0)

我现在已经弄明白了,但我对seg故障的原因很不满意。

我评论了这一行:

Wnck.shutdown()

seg故障消失了。但是,我现在不确定,如果有任何泄漏或其他什么,当我没有正确关闭Wnck时。此外,此关闭之前已经有效。我几天前运行的代码相同。也许一些Ubuntu更新破坏了Wnck或其他东西。

欢迎任何进一步解答导致seg故障的原因。