简体   繁体   English

Python如何为Unittests压缩tkinter mainloop()

[英]Python how to supress tkinter mainloop() for Unittests

Good Morning, 早上好,

I will unittest the following class 我会对下面的课进行单元测试

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

and now i will unittest it, but how can i supress the mainloop() function to run the test and Pass it without initialize the mainloop(), while when the mainloop() starts, it stops the tests and when i close the window i become an error for SystemExit() 现在我将对它进行单元测试,但是我怎么能抑制mainloop()函数来运行测试并传递它而不初始化mainloop(),而当mainloop()启动时,它会停止测试,当我关闭窗口时成为SystemExit()的错误

非常简单:从构造函数中移除start_mainloop调用(无论如何它应该永远不会出现)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM