Py学习  »  Python

Python:日志记录有问题。错误(traceback.format_异常)

Oleg • 4 年前 • 119 次点击  

我有以下代码:

 def tearDown(self):
    e_type, e_value, tb = sys.exc_info()
    if e_type is not None:
        logging.error((traceback.format_exception(e_type, e_value, tb)))

例如,我已经创建了一些用于测试的示例,我希望出现错误。

def test_create_new_user_without_all_fields1(self):
    self.assertEqual('USER_1', 'USER_2')
    logging.info('test_create_new_user_without_all_fields1: Passed')

控制台中的结果:

======================================================================
FAIL: test_create_new_user_without_all_fields1 (test_testExample.BaseTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Dev\git\CST\tests\test_testExample.py", line 16, in test_create_new_user_without_all_fields1
    self.assertEqual('USER_1', 'USER_2')
AssertionError: 'USER_1' != 'USER_2'
- USER_1
+ USER_2
?  +

让我们将小打印添加到tearDown:

print (sys.exc_info())

结果:

(None, None, None)

我们可以看到,现在已经没有例外,但应该是。如何解决这个问题?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/57198
 
119 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Rob Oleg
Reply   •   1 楼
Rob Oleg    4 年前
e_type, e_value, tb = self._outcome.errors[1][1]
logging.error(''.join(traceback.format_exception(e_type, e_value, tb)))