2015-09-04 83 views
0

我試圖在我的PyCharm project上運行pytest測試。測試代碼是下面的: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停止運行,因爲分段故障的任何進一步的測試,所以我不能運行在一個舒適的方式在其他測試。

回答

0

我已經明白了,但我對seg故障的原因還不滿意。

我評論行:

Wnck.shutdown() 

而且賽格故障消失。但是,我現在還不確定,如果有任何泄漏或者什麼時候,我沒有正確關閉Wnck。此外,這種關機已經奏效。我有幾天前運行的相同代碼。也許有些Ubuntu更新在Wnck或其他東西中破壞了某些東西。

歡迎任何進一步的答案,爲什麼這導致seg故障,是值得歡迎的。