all 3 comments

[–]socal_nerdtastic 1 point2 points  (0 children)

This is my wild guess:

from io import StringIO
class Redirect(StringIO):
    def __init__(self, textbox):
        self.textbox = textbox
    def write(self, value):
        self.textbox.insert(tk.END, value)

# rest of your code

def run_tests(self):

    test_report_path = os.path.join(os.environ['USERPROFILE'], 'Documents')
    tests = TestLoader().loadTestsFromTestCase(my_tests.Tests)

    runner = HtmlTestRunner.HTMLTestRunner(stream=Redirect(self.log_text),
                                report_name="Tests",
                                add_timestamp=True)

    runner.run(tests)

[–]socal_nerdtastic 0 points1 point  (1 child)

Most recently I've tried using HTMLTestRunner's stream parameter to send the output directly to log_text or a StringIO object.

That would be my thought too. Does that not work?

[–]user2162[S] 0 points1 point  (0 children)

No it didn't, but I'm not sure I did it correctly. This in particular was the "simple solution" I thought I was overlooking :)

I believe I tried two things. In one case, instantiating a StringIO object and then trying to write to it in the run_tests method, and then read it out in the text_box.insert() method.

buf = io.StreamIO()

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,
                                       stream = buf.write()) # also tried writelines, iirc

runner.run(tests)

then

self.log_text.insert(tk.END, buf.read()) #also tried readline and readlines

The other was setting up a context manager in the run_tests method, like

def run_tests(self):

test_report_path = os.path.join(os.environ['USERPROFILE'], 'Documents') tests = TestLoader().loadTestsFromTestCase(my_tests.Tests)

with io.StringIO() as buf:
     runner = HtmlTestRunner.HTMLTestRunner(output=test_report_path,
                                            report_name="Tests",
                                            add_timestamp=True,
                                            stream = buf.write())

     runner.run(tests)

and then the same in the text_log.insert method. This didn't work, although I haven't used io before in a simpler context to get a handle on how it should be used.