使用 HTMLTestRunner 框架自动生成 HTML 报告, 如下图所示,对于测试失败和出错的用例,测试报告能正常生成,但是报告中未生成失败的信息,不知为何?

主要代码如下:

import sys
from Public import HTMLTestRunner
reload(sys)
sys.setdefaultencoding('utf-8')   #这里是为解决生成报告中文编码问题

now = time.strftime("%Y-%m-%d %H-%M-%S")
report_path = "testResult_TianchengUI_" + now + ".html"
fp = file(report_path, 'wb')
report_runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'测试报告', description=u'用例执行情况:')

suite = unittest.TestSuite()
suite.addTest(TestFraudDetail("test_Fraud_001"))
suite.addTest(TestFraudDetail("test_Fraud_006"))
suite.addTest(TestFraudDetail("test_Fraud_007"))
report_runner.run(suite)

TestFraudDetail 为测试用例类,test_Fraud_xxx 为测试用例方法,部分代码如下:

class TestFraudDetail(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.LoginO = Login()
        cls.LoginO.open()  #打开页面

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.verificationErrors = []
        self.accept_next_alert = True
        self.LoginO.login()  #登录

    def tearDown(self):
        unittest.TestCase.tearDown(self)
        self.assertEqual(self.verificationErrors, [])
        self.LoginO.logout()  #登出

    @classmethod
    def tearDownClass(cls):
        cls.LoginO.close()  #关闭页面

    def test_Fraud_001(self):
        '''反欺诈调用明细查询单个用户数据'''
        po = FraudPage.FraudDetailPage()
        po.InputUserInfo('15112526410')
        po.InputDate(u'全部')
        po.CkQuery()
        self.assertTrue(len(po.GetData()) > 0)

使用 python -m unittest TestFraud 执行结果如下:

.....EF
====================================================================== ERROR: test_Fraud_006 (TestFraud.TestFraudDetail) 姝e父鏌ヨ鎵嬫満閫氳褰曞垎鏋
---------------------------------------------------------------------- Traceback (most recent call last): File "TestFraud.py", line 76, in
test_Fraud_006
po.CkQuery() File "..\PageObject\FraudPage.py", line 499, in CkQuery
self.PO.FindElement("id", selector).Click() File "..\PageObject\Page.py", line 23, in wrapper
ele = func(self, loc_type, loc_value, *locn) File "..\PageObject\Page.py", line 53, in FindElement
ele = self.driverO.find_element(Locate[loc_type], loc_value) File
"C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 712, in find_element
{'using': by, 'value': value})['value'] File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 201, in execute
self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 194, in check_response
raise exception_class(message, screen, stacktrace) NoSuchElementException: Message: no such element: Unable to locate
element: {"method":"id","selector":"submit123"} (Session info:
chrome=47.0.2526.73) (Driver info: chromedriver=2.21.371459
(36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1
x86_64)

====================================================================== FAIL: test_Fraud_007 (TestFraud.TestFraudDetail)
鐢ㄦ埛鍦扮悊浣嶇疆璺熻釜鏌ヨ鐢ㄦ埛鍩烘湰淇℃伅
---------------------------------------------------------------------- Traceback (most recent call last): File "TestFraud.py", line 84, in
test_Fraud_007
self.assertEqual(po.QueryCheck(), '') AssertionError: u'\u59d3\u540d: \u8881\u4f1f\u4f1f
\u8fdb\u5165\u7528\u6237\u753b\u50cf>>\n\u624b\u673a\u53f7\uff1a1511
2526410' != ''

---------------------------------------------------------------------- Ran 7 tests in 51.723s

FAILED (failures=1, errors=1)

enter link description here


↙↙↙阅读原文可查看相关链接,并与作者交流