Python如何抑制单元测试的tkinter mainloop()

问题描述 投票:1回答:1

早上好,

我会对下面的课进行单元测试

class Sample(object):

def __init__(self, dictionary_with_all_button_data, path_to_trunk):
    self.dictionary_with_all_button_data = dictionary_with_all_button_data
    self.path_to_trunk = path_to_trunk
    self.error_main = None
    self.create_mainframe()
    self.create_label()
    self.create_buttons()
    self.start_mainloop()

def create_mainframe(self):
    self.error_main = NewMainFrame()
    self.error_main.get_mainframe().title('Hinweis')
    self.error_main.get_mainframe().geometry(self.get_geometry())

def create_label(self):
    self.note = """        Der Trunk ist noch nicht Commitet, wenn sie fortfahren, 
    erstellen Sie ein Tag das nicht die aktuelle version des Trunks enthält

    Wollen sie einen Automatischen Commit ausführen ? 
    dann drücken sie Ja, 
    drücken Sie Nein um ohne commit fortzufahren, 
    zum Beenden drücken Sie abbrechen"""
    self.hinweis = Label(self.error_main.get_mainframe(), text=self.note, justify=LEFT)
    self.hinweis.place(width=400, height=150)

def create_buttons(self):
    self.yes_button = NewButton(self.error_main.get_mainframe(), 'Ja', 9, 1, 55, 150, '#0A4C82', 'White',
                                ExitButtonEvent().start_Event)
    self.no_button = NewButton(self.error_main.get_mainframe(), 'Nein', 9, 1, 155, 150, '#0A4C82', 'White',
                               OpenFolder(self.path_to_trunk, self.error_main).start_Event)
    self.cancel_button = NewButton(self.error_main.get_mainframe(), 'Abbrechen', 9, 1, 255, 150, '#0A4C82', 'White',
                                   CancelEvent(self.error_main).start_Event)

def start_mainloop(self):
    self.error_main.get_mainframe().mainloop()

def get_geometry(self):
    return '400x200+' + str(self.get_mainframe_horizontal_position()) + '+' + str(
        self.get_mainframe_vertical_position())

def get_mainframe_horizontal_position(self):
    for element in self.dictionary_with_all_button_data['MainFrame']:
        return self.dictionary_with_all_button_data['MainFrame']['mainframe'].get_mainframe().winfo_x()

def get_mainframe_vertical_position(self):
    for element in self.dictionary_with_all_button_data['MainFrame']:
        return self.dictionary_with_all_button_data['MainFrame']['mainframe'].get_mainframe().winfo_y()

现在我将对它进行单元测试,但是我怎么能抑制mainloop()函数来运行测试并传递它而不初始化mainloop(),而当mainloop()启动时,它会停止测试,当我关闭窗口时成为SystemExit()的错误

python python-3.x unit-testing tkinter
1个回答
3
投票

非常简单:从构造函数中删除start_mainloop调用(无论如何它都不应该在那里)

© www.soinside.com 2019 - 2024. All rights reserved.