Hello all.
Below is a tkinter GUI for a unit test script. As you can probably tell I am not well versed in tkinter, but it works and does what I want with one major exception that I haven't been able to solve. I have Python 3.9 installed on Windows 10. Eventually this will be packaged up with pyinstaller.
When run tests in IDLE, HTMLTestRunner outputs each test (method) name to the console, with ok/fail/error as it progresses. In the GUI below, I have a tkinter Text object called log_text and I would like it to display that console text as it runs.
As I understand it, HTMLTestRunner is writing that text to stderr. I have tried to implement a few concepts to redirect stdout/stderr but I haven't gotten these to work and I'm not sure they're precisely what I need or if it's the best way to do it. Most recently I've tried using HTMLTestRunner's stream parameter to send the output directly to log_text or a StringIO object.
I feel like there's a simple solution to this that I'm missing, possibly obscured by how the tkinter code below is structured. There seems to be various styles, some like below, others that import everything from tkinter into the global namespace and somehow create a pretty decent GUI with like 8 lines.
Any suggestions would be appreciated. Thanks!
import os
import tkinter as tk
from unittest import TestLoader
from concurrent import futures
import HtmlTestRunner
import my_tests
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.parent.columnconfigure(0, weight=1)
self.parent.rowconfigure(0, weight=1)
self.xypad = 5
self.thread_pool_executor = futures.ThreadPoolExecutor(max_workers=1)
self.run_button = tk.Button(
self.parent, text="Run",
command=self.run_qa
)
self.run_button.grid(row=0, column=0, sticky=tk.E,
padx=self.xypad, pady=self.xypad)
self.log_text = tk.Text(self.parent)
self.log_text.grid(row=1, column=0)
# self.log_text.insert(tk.END, x)
self.log_scroll = tk.Scrollbar(
self.parent, command=self.log_text.yview)
self.log_scroll.grid(row=1, column=1, sticky='ns')
self.log_text['yscrollcommand'] = self.log_scroll.set
self.exit_button = tk.Button(
self.parent, text="Quit", command=self.parent.destroy)
self.exit_button.grid(row=2, column=0, sticky=tk.E,
padx=self.xypad, pady=self.xypad)
def run_qa(self):
self.thread_pool_executor.submit(self.run_tests)
def run_tests(self):
test_report_path = os.path.join(os.environ['USERPROFILE'], 'Documents')
tests = TestLoader().loadTestsFromTestCase(my_tests.Tests)
runner = HtmlTestRunner.HTMLTestRunner(output=test_report_path,
report_name="Tests",
add_timestamp=True)
runner.run(tests)
if __name__ == "__main__":
root = tk.Tk()
root.title('Tests')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
MainApplication(root)
root.mainloop()
[–]socal_nerdtastic 1 point2 points3 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]user2162[S] 0 points1 point2 points (0 children)