Python unittest

Re: Python unittest

by Peter Sander -
Number of replies: 0

Bingo! Intercepting the output from unittest does the trick. And I have to diddle that output anyway to neutralize the displayed time since the test run times would vary between users. Here's my quick hack:

if __name__ == '__main__':
# find all tests in this module
import __main__
suite = unittest.TestLoader().loadTestsFromModule(__main__)
with io.StringIO() as buf:
# run the tests
with contextlib.redirect_stdout(buf):
unittest.TextTestRunner(stream=buf).run(suite)
# process the results - neutralise the time taken
# since this can vary depending on the phases of the moon
result = buf.getvalue()
start = result.find(' in ') + len(' in ')
end = result.find('s', start, len(result))
result = result.replace(result[start:end], 'X.XXX')
print(result)
It needs re-packaging into a more useful form. What's a bit curious is that it redirects stdout (also works with stderr), but it works.

Thanks a bunch for your quick and insightful help!