1.代碼以下所示:html
from selenium import webdriver import unittest import HTMLTestRunner class BaiduSearch(unittest.TestCase): def setUp(self): self.driver = webdriver.Ie() self.driver.get('http://www.baidu.com') self.driver.implicitly_wait(3) def test_baidu(self): driver = self.driver driver.find_element_by_id('kw').send_keys('A') driver.find_element_by_id('su').click() def tearDown(self): self.driver.quit() if __name__ == '__main__': suit = unittest.TestSuite() #測試套件 suit.addTest(BaiduSearch('test_baidu')) #將測試用例添加到測試套件 filename = 'E:\\nancy.html' fp = open(filename, 'wb') testRunner = HTMLTestRunner.HTMLTestRunner(stream=fp,title='啦啦啦啦啦啦') #定義測試報告 testRunner.run(suit) #運行 fp.close()
2.右鍵運行後沒法生成測試報告解決方法:python
(1)filename的命名格式:android
A:filename = 'E:\\testresult.html’
B:filename = r'E:\testresult.html'
C:filename = 'E:/testresult.html'
(2)fp.close()是否在代碼中編寫web
(3)Pycharm IDE的問題,查看博客:https://www.cnblogs.com/android-it/p/9145813.html測試
(4)按照如下方法操做:ui
A:在Pycharm的右上角==選擇Edit Configurationhtm
B:添加python腳本,選擇相對應的位置blog
C:選擇保存的python腳本,運行ci
3.運行成功後在指定位置能夠查看生成的測試報告,以下所示:element