Redirecting unittest output to a Text widget

M

MrBlueSky

Morning! I'm writing my first Python program, so please have patience!

I'd like to redirect the output from my application's unit tests
("import unittest") to a Tkinter Text object. I found the magic to
redirect stdout and stderr:
sys.stdout = myTextWindow
sys.stderr = myTextWindow
where myTextWindow inherits from Text and implements write() and
writelines().

But the output from my UT still appears in the command window, not the
Text box. My own "print" statements *do* appear in the Text box.

Can anyone suggest a way forward here?

John
 
P

Peter Otten

MrBlueSky said:
Morning! I'm writing my first Python program, so please have patience!

I'd like to redirect the output from my application's unit tests
("import unittest") to a Tkinter Text object. I found the magic to
redirect stdout and stderr:
sys.stdout = myTextWindow
sys.stderr = myTextWindow
where myTextWindow inherits from Text and implements write() and
writelines().

But the output from my UT still appears in the command window, not the
Text box. My own "print" statements *do* appear in the Text box.

Can anyone suggest a way forward here?

Provide your file-like object to an explicitly instantiated TextTestRunner:

tr = unittest.TextTestRunner(stream=myTextWindow, verbosity=2)
unittest.main(testRunner=tr)

If you want to accept a verbosity option from the commandline you have to
subclass unittest.TestProgram and override its runTests() method,
unfortunately.

Peter
 
M

MrBlueSky

Excellent, that seems to have done the trick!

FWIW: I should admit that I haven't really taken the time to properly
understand unittest... I'm too eager to get my tests written :)
So for reasons I do not as yet understand Peter's suggestion above
didn't quite work for me but the following did....

testSuite = unittest.makeSuite(testX)
testSuite = unittest.makeSuite(testY)
testRunner = unittest.TextTestRunner(stream=myTextWindow,
verbosity=2)
testRunner.run(testSuite)

I'm only mentioning this in case it's relevant to others.

Thanks, Peter!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top