selenium自学之路:自动化测试高级应用之生成HTM测试报告

from selenium import webdriver
import unittest
from HTMLTestRunner import HTMLTestRunner

class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver=webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.base_url = 'http://www.baidu.com/'

    def test_baidu_search(self):
        driver=self.driver
        driver.get(self.base_url)
        driver.find_element_by_id('kw').send_keys('HTMLTestRunner')
        driver.find_element_by_id('su').click()

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    testunit=unittest.TestSuite()
    testunit.addTest(Baidu('test_baidu_search'))
    fp=open('./result.html','wb')
    runner=HTMLTestRunner(stream=fp,title='百度搜索测试',description='用例执行情况')
    runner.run(testunit)
    fp.close()

运行时会报错:

.Traceback (most recent call last):
  File "C:/Users/pwrd/.PyCharmCE2018.2/config/scratches/seleniumtest.py", line 25, in 
    runner.run(testunit)
  File "C:\Users\pwrd\AppData\Local\Programs\Python\Python36\lib\HTMLTestRunner.py", line 630, in run
    self.generateReport(test, result)
  File "C:\Users\pwrd\AppData\Local\Programs\Python\Python36\lib\HTMLTestRunner.py", line 677, in generateReport
    report = self._generate_report(result)
  File "C:\Users\pwrd\AppData\Local\Programs\Python\Python36\lib\HTMLTestRunner.py", line 712, in _generate_report
    sortedResult = self.sortResult(result.result)
  File "C:\Users\pwrd\AppData\Local\Programs\Python\Python36\lib\HTMLTestRunner.py", line 642, in sortResult
    if not rmap.has_key(cls):
AttributeError: 'dict' object has no attribute 'has_key'

点击错误打开HTMLTestRunner.py文件
定位问题:

 if not rmap.has_key(cls):

由于python3删除了has_key这个方法,因此改为:

if cls not in rmap:

此时重新运行程序:

.<_io.TextIOWrapper name='' mode='w' encoding='UTF-8'> 
Time Elapsed: 0:00:06.767387

查看文件所在目录就可以看到生成的log文件了

执行某个文件夹下的所有用例生成报告

import unittest,time
from HTMLTestRunner import HTMLTestRunner

test_dir='./test_case'
discover=unittest.defaultTestLoader.discover(test_dir,pattern='test_*.py')
if __name__ == '__main__':
    now=time.strftime("%Y-%m-%d %H_%M_%S")
    filename=test_dir+'/'+now+'result.html'
    fp=open(filename,'wb')
    runner=HTMLTestRunner(stream=fp,title='测试报告',description='用例执行情况:')
    runner.run(discover)
    fp.close()

你可能感兴趣的:(selenium)