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

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

早上好,

我将对以下类(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()
现在我将对它进行单元测试,但是如何抑制 mainloop() 函数来运行测试并在不初始化 mainloop() 的情况下通过它,而当 mainloop() 启动时,它会停止测试,当我关闭时该窗口成为 SystemExit() 的错误

最佳答案

很简单:从构造函数中删除 start_mainloop 调用(无论如何它都不应该出现在此处)

关于Python 如何抑制 tkinter mainloop() 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50712657/

相关文章:

Python 卡在 Selenium webdriver.Chrome()

python - 停止 qthread 中长时间运行的进程

python - tf.reshape 没有为第一个元素提供 ?(None)

ruby-on-rails - 如何获取查询以返回我的模拟对象?

python - 基于pandas dfs中的部分字符串匹配进行合并

python - 有人可以向我解释函数对象和闭包之间的区别吗

python - pygame在窗口未聚焦时捕获键盘事件

Python 3 - Pipenv 无法安装出现错误

unit-testing - Racket 哈希相等

android - 这是在单元测试中使用 Dagger 2 for Android 应用程序以使用模拟/伪造覆盖依赖项的正确方法吗?