Use python+openpyxl to read multiple data of test cases from the table, then write the execution results into the table, and generate test reports at the same time

1  # -*- coding: utf-8 -*- 
2  import unittest
 3  from selenium import webdriver
 4  import HTMLTestRunner
 5  from openpyxl import load_workbook
 6  class mylogintest(unittest.TestCase): #Define a mylogintest class to inherit unittest.TestCase class 
7      ' '' Open Baidu test ''' 
8      def setUp(self): #Initialization work before execution of the use case 
9          print ( " Start test " )
 10          self.driver =webdriver.Firefox()
 11  
12      def tearDown(self): #Initialization work after execution of the use case 
13          print ( " End test " )
 14          self.driver.quit()
 15  
16      def testopenBD(self):
 17          ''' Open Baidu ''' 
18          book = load_workbook( " mylogintest.xlsx " )   #Read and write by default, if necessary, you can specify write_only and read_only as True 
19          b = book.active
 20          datadict = {} #Create a dictionary variable 
21          maxC = b .max_column#Get the maximum column 
22          maxR = b.max_row #Get the maximum row 
23          for i in range(1, maxC + 1): # The range starts from 0 by default and ends at -1 of the following parameter, and openpyxl starts from the first row The first column starts, so the parameter is 1, maxC+1; it means to traverse the first column to the last column, 
24              datadict.setdefault(b.cell(1, i).value) #Set the key value of the dictionary datadict, from The first column of the first row, to the last column of the first row, that is, the title of the first row is set as the key value, i means column 
25              # print(datadict) 
26          for i in range(2, maxR + 1): #Remove The title of the first row, traverse from the second row to the last row 
27              for j in range(1, maxC + 1): # Traverse from the first column to the last column 
28                 datadict[b.cell(1, j).value] = b.cell(i, j).value #Set the value corresponding to the key in the dictionary, j represents column 
29                  # print(datadict) 
30              # if datadict["url "] & datadict["check"] != None: 
31              self.driver.get(datadict[ " address " ]) #Get the URL value corresponding to the value address from the corresponding key, where the key is the first row of the table title, so the title of EXCEL should be written according to the regulations 
32              if self.driver.title == datadict[ " check " ]: #If the address is opened, the title value is the same as the value corresponding to the check 33                  b.cell(i, 4, ' Pass ' ) #Write the result of passing the test into the table 34 else :


             35                  b.cell(i, 4, ' failed ' ) #Write the test failure result into the table 
36          book.save( " mylogintest.xlsx " ) # Finally remember to close 
37  
38  if  __name__ == ' __main__ ' :
 39      filename = ' ./ ' + ' mylogintestresult.html ' #Define file name and path 
40      fp = open(filename, ' wb ' ) #Generate file 
41      ut = unittest.TestSuite()  #Create a test suite; TestSuite results without parentheses: addTest() missing 1 required positional argument: 'test' 
42      ut.addTest(mylogintest( ' testopenBD ' )) #Add the test case to be executed to the test suite 
43      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 is enough 
44      runner.run (ut) #run test suite 
45      fp.close()

The content of the table is: The yellow mark is the value automatically written by the script

testing report:

In fact, for a use case with multiple pieces of data, it is not very useful to use a test report, and I have not found a test case for the time being. Multiple pieces of test data can generate a report with multiple results. There is only one here, so write it directly in a table. The method of writing and writing is also more convenient.

The generated test report can be deleted and not used

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325383191&siteId=291194637