Marcin Nowicki
Marcin Nowicki

Reputation: 33

python - unit tests logging result file

I need logging to file tests result which is displayed in the console: 'Ran 1 test in 83.904s OK '

my method:

 def allTests(self):
    testmodules = [
        #'tests.tests1',
        'tests.tests2',
        'tests.tests3'    
    ]
    suite = unittest.TestSuite()
    results = unittest.TextTestRunner(verbosity=2).run(suite)
    for t in testmodules:
        suite.addTest(unittest.defaultTestLoader.loadTestsFromName(t))
        print results
    unittest.TextTestRunner().run(suite)

and i run my test:

if __name__ == '__main__':
timestr = time.strftime("%d%m%Y-%H%M%S")
logFormatter = log.Formatter("%(asctime)s [%(levelname)s]  %(message)s")
rootLogger = log.getLogger()
logPath = './Logs/'
fileName = '{0}--StartTests'.format(timestr)
fileHandler = log.FileHandler("{0}/{1}.log".format(logPath, fileName))
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = log.StreamHandler(sys.stdout)
rootLogger.setLevel(log.NOTSET)
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
log.disable(log.DEBUG)
start = startTests()
start.allTests()

I have all information logged except information about end result (OK,Fail Skipped)

Ran 1 test in 72.710s Fail (Fail=1)

How can I add this information ???

Please Help me, THX.

Upvotes: 1

Views: 572

Answers (1)

Marcin Nowicki
Marcin Nowicki

Reputation: 33

it works i add to : unittest.TextTestRunner().run(suite) >>> log.info( unittest.TextTestRunner().run(suite))

Upvotes: 1

Related Questions