启动单元测试后,GUI没有响应

时间:2020-05-31 12:39:01

标签: python python-3.x user-interface tkinter

我编写了一些使用Selenium的单元测试,并为此创建了Tkinter GUI。

length = bin1Length; if (bin2Length > bin1Length) length = bin2Length; 是:

script.py

interface.py是:

from selenium import webdriver
import unittest

class LaunchChrome(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome(r"C:\Users\USERNAME\PycharmProjects\First\Drivers\chromedriver.exe")
        cls.driver.maximize_window()
        cls.driver.get('https://facebook.com')

if __name__ == '__main__':
    unittest.main()

打开GUI并单击“开始”按钮后,单元测试可以正常工作,但GUI停止响应(不响应):

(Not Responding)

我如何使用多处理代码?

1 个答案:

答案 0 :(得分:3)

您需要创建一个线程来在那里运行测试。但是有必要.start()。将 interface.py 文件替换为:

import tkinter as tk
import tkinter.ttk as ttk
import unittest
from threading import Thread

from ex import LaunchChrome


class HomeApplication(tk.Tk):
    def __init__(self):
        super().__init__()

        ttk.Button(self, text="Start", width=60, command=self.start_app)\
                     .grid(row=4, column=0, columnspan=3)

    def run_tests(self):
        test_suite = unittest.TestSuite([unittest.TestLoader()\
                                         .loadTestsFromTestCase(LaunchChrome)])
        unittest.TextTestRunner(verbosity=2).run(test_suite)

    def start_app(self):
        Thread(target=self.run_tests, daemon=True).start()

if __name__ == "__main__":
    HomeApplication().mainloop()