1

I found the below code here on stackoverflow in order to write my python unittest results to a text file. Per the python unittest documentation the first argument in unittest.TextTestRunner is the stream. So, f which represents the text file is where the output goes. When I run this I see that the console no longer displays the output. How can I get the unittest results to display in both the console AND the text file at the same time? Thank you very much.

log_file = 'log_file.txt'
with open(log_file, "w") as f:
    runner = unittest.TextTestRunner(f, verbosity=2)
    unittest.main(testRunner=runner)
SAB15
  • 41
  • 1
  • 4
  • It looks like many people prefer to leave the std.out stream alone in the python code and use the operating system, specifically in Unix ,`tee`, to log a copy of the screen output to a file. See [How to duplicate sys.stdout to a log file?](https://stackoverflow.com/questions/616645/how-to-duplicate-sys-stdout-to-a-log-file) – PeterK Dec 07 '20 at 00:31
  • Please consider splitting into two questions, since you ask two questions. – PeterK Dec 07 '20 at 00:35
  • @PeterK Created separate question per your suggestion. Thanks. – SAB15 Dec 08 '20 at 15:35

0 Answers0