Python如何为Unittests压缩tkinter mainloop()

时间:2018-06-06 05:13:56

标签: python python-3.x unit-testing tkinter

早上好,

我将对以下课程进行单元测试

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()

的错误

1 个答案:

答案 0 :(得分:3)

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