1.代码如下所示:
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.右键运行后无法生成测试报告解决方法:
(1)filename的命名格式:
A:filename = 'E:\\testresult.html’
B:filename = r'E:\testresult.html'
C:filename = 'E:/testresult.html'
(2)fp.close()是否在代码中编写
(3)Pycharm IDE的问题,查看博客:https://www.cnblogs.com/android-it/p/9145813.html
(4)按照以下方法操作:
A:在Pycharm的右上角==选择Edit Configuration
B:添加python脚本,选择相对应的位置
C:选择保存的python脚本,运行
3.运行成功后在指定位置可以查看生成的测试报告,如下所示: