[Tutor] redirecting testing output to file, using two different options

Yosef Levy yosi.levy99 at gmail.com
Wed May 16 04:53:25 EDT 2018


Hello All,

I have testing environment.
Code is written under Python 2.6.
testing output should be redirected to log file.
I am using two different file handling, in order to log output:



1. logging module:

import unittest2
import logging
import sys

class TestMyEnvClass (unittest2.TestCase):
    def setUp (self):
        pass
    def testMyEnv (self):
        logging.debug  ( "In testMyEnv.." )
    def tearDown (self):
        pass

if __name__ == "__main__":
    logging.basicConfig( filename='log_file.txt', stream=sys.stderr,
level=logging.DEBUG)
    logging.getLogger( "TestMyEnvClass.testMyEnv" ).setLevel( logging.DEBUG
)
    unittest2.main()


running:
python test.py

output:
# cat log_file.txt
DEBUG:root:In testMyEnv..
#





2. but if I want to redirect assert messages to file, I have to:

import unittest2

class TestMyEnvClass (unittest2.TestCase):
    def setUp (self):
        pass
    def testMyEnv (self):
        res = True
        self.assertEqual(res, True, 'Testing my environment..')
    def tearDown (self):
        pass

if __name__ == "__main__":
    f = open('log_file.txt', "a")
    runner = unittest2.TextTestRunner(f)
    unittest2.main(testRunner=runner)
    f.close ()



running:
# python test2.py

output:
# cat log_file.txt
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK



Question:
Is the a way to 'merge' the above two different ways output to file?
Or, is there a way to redirect assert messages to logging module methods?


Rgds.


More information about the Tutor mailing list