wxPython GUI测试

时间:2012-08-10 15:35:04

标签: testing wxpython

如何处理在测试wxPython应用程序时打开的对话框?

已有人a similar issue

  

问题是,只要应用程序启动模态对话框,就可以控制   在模态对话框退出之前不会返回,此时它就是   测试脚本输入数据的时间太晚了

总的来说,我想为以下工作流程编写测试用例:

  1. 用户按下按钮“SomeProcessing”
  2. 在打开的对话框中,用户选择“选择1”并按“确定”
  3. 根据选择处理数据,并与已知结果(data_after_processing
  4. 进行比较

    如何执行第2步以便自动执行操作(以下示例打开Dlg_GetUserInput并等待手动输入)?可能是,我对GUI测试的理解存在缺陷,第3部分不应被视为GUI测试?在那种情况下,我可能需要重写代码......

    欢迎任何建议!

    import wx
    
    class MyFrame(wx.Frame):
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, title=title)
            btn = wx.Button(self, label="SomeProcessing")
            self.Bind(wx.EVT_BUTTON, self.SomeProcessing, btn)
    
        def SomeProcessing(self,event):
            self.dlg = Dlg_GetUserInput(self)
            if self.dlg.ShowModal() == wx.ID_OK:
                if self.dlg.sel1.GetValue():
                    print 'sel1 processing'
                    self.data_after_processing = 'boo'
                if self.dlg.sel2.GetValue():
                    print 'sel2 processing'
                    self.data_after_processing = 'foo'
    
    class Dlg_GetUserInput(wx.Dialog):
        def __init__(self, parent):
            wx.Dialog.__init__(self, parent)
            self.sel1 = wx.CheckBox(self, label='Selection 1')
            self.sel2 = wx.CheckBox(self, label='Selection 2')
            self.OK = wx.Button(self, wx.ID_OK)
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.sel1)
            sizer.Add(self.sel2)
            sizer.Add(self.OK)
            self.SetSizer(sizer)
    
    def test():
        app = wx.PySimpleApp()
        mf = MyFrame(None, 'testgui')
    
        for item in mf.GetChildren():
            if item.GetLabel() == 'SomeProcessing':
                btn = item
                break
    
        event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())   
        mf.GetEventHandler().ProcessEvent(event)
    
        """
        PROBLEM: here I'd like to simulate user input 
        sel1 in Dlg_GetUserInput 
        (i.e. mf.dlg.sel1.SetValue())
        and check that 
        data_after_processing == 'boo'
        """
    
        mf.Destroy()
    
    test()  
    

2 个答案:

答案 0 :(得分:3)

您可能想要查看其中一个用于GUI测试的应用程序:

答案 1 :(得分:0)

发布解决方案,以防任何人遇到同样的问题。

def test():
    app = wx.PySimpleApp()
    mf = MyFrame(None, 'testgui')
    for item in mf.GetChildren():
        if item.GetLabel() == 'SomeProcessing':
            btn = item
            break

    def clickOK():
        dlg = wx.GetActiveWindow()
        dlg.sel1.SetValue(True)
        clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, wx.ID_OK)
        dlg.ProcessEvent(clickEvent)

    event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())   
    wx.CallAfter(clickOK)
    mf.GetEventHandler().ProcessEvent(event)

    print 'data_after_processing:', mf.data_after_processing
    mf.Destroy()