1 # -*- coding: utf-8 -*- 2 import unittest 3 import time 4 from selenium import webdriver 5 import xlrd 6 import HTMLTestRunner 7 8 class mylogintest(unittest.TestCase): 9 ''' Open Baidu Test ''' 10 11 # @classmethod 12 # def tearDownClass(cls): #Initialization work after the execution of the use case 13 # print('End test') 14 15 def setUp(self): #Initialization work before the execution of the test case 16 print ( " start test " ) 17 self.driver = webdriver.Firefox() 18 19 def tearDown(self): #Initialization work after execution of the test case 20 print ( " End test " ) 21 self. driver.quit() 22 23 def testopenBD(self): 24 ''' Open Baidu ''' 25 book = xlrd.open_workbook( " mylogintest.xlsx " ) #Create a new file object 26 sheet = book.sheet_by_name( " login " ) #Get the sheet object 27 whose sheet name is login in the file object # # # url1 = sheet.cell(1, 0).value Get the value of row 2, column 1 28 # # # url2 = sheet.cell(1, 1).value Get the value of row 2 and column 2 29 nrows = sheet.nrows #Get the total number of rows 30 urllist = [] 31 for i in range(1 , nrows): 32 print (i) 33 nrowsvalue = sheet.row_values(i, 0) #Get the value of row i+1, column 1 34 urllist.append(nrowsvalue) # Adding elements here cannot use list = list.append(nrowsvalue); otherwise, an error of nonetype will occur; 35 print (urllist) 36 driver = self.driver 37 for i in range(len(urllist)): #Get the length of the list; range Integer iteration starting from 0 by default 38 print (urllist[i]) 39 driver.get( '' .join(urllist[i])) #Convert list type to string type 40 print (driver.title) 41 self. assertEqual(driver.title, ' Baidu, you will know ' , ' failure ' ) #Add an assertion; when the value of driver.title is 'Baidu, you will know', the assertion is passed, otherwise it will not pass, the program will be terminated 42 # time.sleep(3) 43 44 # @classmethod 45 # def setUpClass( cls):##Initialization work before use case execution 46 # print('Start test') 47 48 if __name__ == ' __main__ ' : 49 filename = ' ./ ' + ' mylogintestresult.html ' #Define file name and path 50 fp = open(filename, ' wb ' ) #Generate file 51 ut = unittest.TestSuite() #Create a test suite; TestSuite results without parentheses: addTest() missing 1 required positional argument: 'test' 52 ut.addTest(mylogintest( ' testopenBD ' )) #Add the test case to be executed to the test 53 runner = HTMLTestRunner.HTMLTestRunner (stream=fp, title= ' Open Baidu 51 website respectively ' , description= ' Baidu 51 ' ) #If the reference is from HTMLTestRunner import HTMLTestRunner, there is no need to write two HTMLTestRunners here, one Enough 54 runner.run(ut) #Run the test suite 55 fp.close()
Generated report: