Test report generation (4)

  After writing the test code using python+selenium, test the web interface to be tested. After the test is completed, the next step to consider is how to generate a test report from the test result information.

  I found a lot of information on the Internet, and found that when everyone used HTMLTestRunner.py to generate test reports, the download address

  1. Modify the HTMLTestRunner.py file

  After downloading, it is found that this file is written in python2, while the local one is python3, so the code needs to be modified. The modification content is:

  1. In line 94, import StringIO is changed to: import io, and correspondingly, line 539 is changed to: self.outputBuffer = io.BytesIO()

  2. Line 118, self.fp.write(s) is modified to self.fp.write(bytes(s,'UTF-8')) 

  3、631行,print >>sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改为 print('\nTime Elapsed: %s' % (self.stopTime-self.startTime),file=sys.stderr)

  4. Line 642, if not rmap.has_key(cls): modified to: if not cls in rmap:

  5. Line 766, uo = o.decode('latin-1') is changed to uo = o, line 768, uo = o is changed to uo = o.decode('UTF-8')

     Line 772, ue = e.decode('latin-1') is changed to ue = e, line 774, ue = e is changed to ue = e.decode('UTF-8')

  As for why this modification is made, python3 and python2 have some grammatical differences. You can locate them according to the grammatical problems that arise.

  2. Import

  Place the HTMLTestRunner.py file in the lib folder under python35.

  If the local Python lib file directory cannot be found, as shown in the figure, copy the file into the lib directory

  

  Then  import HTMLTestRunner, when you enter import, you can see a prompt appears.

   3. Code testing

 1 #coding=utf-8
 2 from selenium import webdriver
 3 from selenium.webdriver.common.by import By
 4 from selenium.webdriver.support.ui import Select
 5 
 6 import unittest
 7 import time
 8 import HTMLTestRunner
 9 
10 class JD(unittest.TestCase):
11     def setUp(self):
12         self.browser=webdriver.Chrome()
13         self.browser.implicitly_wait(30)
14         self.base_url="https://www.jd.com/"
15         self.verficationErrors=[]
16         self.accept_next_alert=True
17     def Login(self):
18         browser=self.browser
19         browser.get(self.base_url+'/')
20         browser.maximize_window()
21         browser.execute_script('javascript:login()')
22 if __name__=="__main__":
23      # unittest.main() 
24      testunit= unittest.TestSuite()
 25      #Add the test case to the test container 
26      testunit.addTest(JD( " Login " ))
 27      #Get the current time, which is convenient for the following use. 
28      now = time.strftime( " %Y-%m-%M-%H_%M_%S " ,time.localtime(time.time()))
 29      #Open a file and write the result to this file 
30      fp=open( " result " +now+ " .html " , ' wb ' )
 31     runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title='test result',description=u'result:')
32     runner.run(testunit)
33     fp.close()

  4. Test report

  The test report interface is a bit ugly and will be improved in the future. The result of executing the use case is error. Improve it!

Guess you like

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