Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Edited title and removed language tag.
Source Link
martineau
  • 124.1k
  • 29
  • 181
  • 319

python - how How can I redirect the output of unittest? Obvious solution doesn't work

python - how How can I redirect the output of unittest? Obvious solution doesn't work

Here is my code:

import unittest import sys import os class DemoTest(unittest.TestCase): def test_one(self): print "test one" self.assertTrue(True) def test_two(self): print "test two" self.assertTrue(False) if __name__ == '__main__': dirpath = os.path.dirname(os.path.abspath(__file__)) sys.stdout = open(dirpath+'/test_logs/demo_test.stdout.log', 'w') sys.stderr = open(dirpath+'/test_logs/demo_test.stderr.log', 'w') test_program = unittest.main(verbosity=0, exit=False) 

When I run this, the contents of demo_test.stdout.log is only:

test one test two 

on the screen I still see the output from unittest:

====================================================================== FAIL: test_two (__main__.DemoTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "demotest.py", line 12, in test_two self.assertTrue(False) AssertionError: False is not true ---------------------------------------------------------------------- Ran 2 tests in 0.000s FAILED (failures=1) 

I want there to be no output on the screen and everything to be logged. (I am running the test as a cron job, so any output to stdout or stderr causes an email to be sent, so I want to be able to specify exactly when this happens, which means I need to be able to control unittest in this regard.)

python - how can I redirect the output of unittest? Obvious solution doesn't work

Here is my code:

import unittest import sys import os class DemoTest(unittest.TestCase): def test_one(self): print "test one" self.assertTrue(True) def test_two(self): print "test two" self.assertTrue(False) if __name__ == '__main__': dirpath = os.path.dirname(os.path.abspath(__file__)) sys.stdout = open(dirpath+'/test_logs/demo_test.stdout.log', 'w') sys.stderr = open(dirpath+'/test_logs/demo_test.stderr.log', 'w') test_program = unittest.main(verbosity=0, exit=False) 

When I run this, the contents of demo_test.stdout.log is only:

test one test two 

on the screen I still see the output from unittest:

====================================================================== FAIL: test_two (__main__.DemoTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "demotest.py", line 12, in test_two self.assertTrue(False) AssertionError: False is not true ---------------------------------------------------------------------- Ran 2 tests in 0.000s FAILED (failures=1) 

I want there to be no output on the screen and everything to be logged. (I am running the test as a cron job, so any output to stdout or stderr causes an email to be sent, so I want to be able to specify exactly when this happens, which means I need to be able to control unittest in this regard.)

How can I redirect the output of unittest? Obvious solution doesn't work

Here is my code:

import unittest import sys import os class DemoTest(unittest.TestCase): def test_one(self): print "test one" self.assertTrue(True) def test_two(self): print "test two" self.assertTrue(False) if __name__ == '__main__': dirpath = os.path.dirname(os.path.abspath(__file__)) sys.stdout = open(dirpath+'/test_logs/demo_test.stdout.log', 'w') sys.stderr = open(dirpath+'/test_logs/demo_test.stderr.log', 'w') test_program = unittest.main(verbosity=0, exit=False) 

When I run this, the contents of demo_test.stdout.log is only:

test one test two 

on the screen I still see the output from unittest:

====================================================================== FAIL: test_two (__main__.DemoTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "demotest.py", line 12, in test_two self.assertTrue(False) AssertionError: False is not true ---------------------------------------------------------------------- Ran 2 tests in 0.000s FAILED (failures=1) 

I want there to be no output on the screen and everything to be logged. (I am running the test as a cron job, so any output to stdout or stderr causes an email to be sent, so I want to be able to specify exactly when this happens, which means I need to be able to control unittest in this regard.)

Source Link
tadasajon
  • 14.9k
  • 30
  • 101
  • 148

python - how can I redirect the output of unittest? Obvious solution doesn't work

Here is my code:

import unittest import sys import os class DemoTest(unittest.TestCase): def test_one(self): print "test one" self.assertTrue(True) def test_two(self): print "test two" self.assertTrue(False) if __name__ == '__main__': dirpath = os.path.dirname(os.path.abspath(__file__)) sys.stdout = open(dirpath+'/test_logs/demo_test.stdout.log', 'w') sys.stderr = open(dirpath+'/test_logs/demo_test.stderr.log', 'w') test_program = unittest.main(verbosity=0, exit=False) 

When I run this, the contents of demo_test.stdout.log is only:

test one test two 

on the screen I still see the output from unittest:

====================================================================== FAIL: test_two (__main__.DemoTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "demotest.py", line 12, in test_two self.assertTrue(False) AssertionError: False is not true ---------------------------------------------------------------------- Ran 2 tests in 0.000s FAILED (failures=1) 

I want there to be no output on the screen and everything to be logged. (I am running the test as a cron job, so any output to stdout or stderr causes an email to be sent, so I want to be able to specify exactly when this happens, which means I need to be able to control unittest in this regard.)