Python uses HTMLTestRunner. py to generate a test report,
This article describes how to use HTMLTestRunner. py to generate a test report in python. The details are as follows:
HTMLTestRunner. py python 2
: Http://tungwaiyip.info/software/HTMLTestRunner.html
Create a "PyDev Package" to copy the downloaded HTMLTestRunner. py file to this directory.
Example: testcase5_dynamic.py
import unittestfrom dev.widget import Widgetclass WidgetTestCase(unittest.TestCase): def setUp(self): self.widget=Widget() def tearDown(self): self.widget.dispose() self.widget=None def testSize(self): self.assertEqual(self.widget.getSize(), (40,40), "Wrong") def testResize(self): self.widget.resize(100, 100) self.assertEqual(self.widget.getSize(), (100,100), "Wrong")
Html_report.py:
#coding:utf-8from lib import HTMLTestRunnerimport unittestfrom testcase5_dynamic import WidgetTestCaseif __name__=='__main__': suite=unittest.makeSuite(WidgetTestCase) filename='D:\\myreport.html' fp=file(filename,'wb') runner=HTMLTestRunner.HTMLTestRunner(fp,title=u'my unit test',description=u'This is a report test') runner.run(suite)
When running, you need to use Python Run, and use Python unit-testto Run the test to generate myreport.html. Currently, I do not know why.
Sometimes, D: \ myreport.html is not generated immediately. We can create an empty myreport.html file first, so that the report content will be displayed after it is run.
Python 3 of HTMLTestRunner. py
Because HTMLTestRunner. py was originally in Python 2, we have not found Python 3 yet. Therefore, we need to modify the HTMLTestRunner. py file by ourselves.
1. Modified location
Question 1: No module named StringIO
Cause: There is no StringIO module in python 3. Here we need to use the io module instead.
Solution:
The name introduced in row 94th should be changed from import StringIO to import io.
Correspondingly, the first line of self. outputBuffer = StringIO. StringIO () should be changed to self. outputBuffer = io. BytesIO ()
Problem 2: AttributeError: 'dict 'object has no attribute 'has _ key'
Cause: python 3 dictionary objects do not support the has_key function. We need to use in for traversal.
Solution:
Locate row 642. if not rmap. has_key (cls): replace it with if not cls in rmap:
Question 3: 'str' object has no attribute 'decode'
Cause: In python3, decode has been removed from character operations.
Solution:
Locate row 772 and change ue = e. decode ('Latin-1') to ue = e.
In addition, 766 there is a similar uo = o. decode ('Latin-1 '), which is changed to uo = o;
Problem 4: TypeError: can't concat bytes to str
Cause: locate and report the content in line 1, escape (uo + ue ). This is because when we assign a value to uo, we use the else process, and uo is assigned a value of the bytes type. The bytes type cannot be directly converted to the str type. Therefore, we need to convert the bytes type to the str type before assigning values to uo.
Solution:
Change the uo = o of Row 3 to uo = o. decode ('utf-8 ').
In addition, there is a similar ue = e in 774, which is changed to ue = e. decode ('utf-8 ').
Question 5: TypeError: unsupported operand type (s) for >>: 'builtin _ function_or_method 'and 'rpcproxy'
Cause: python3 does not support print> sys. stderr. If output stream is defined here, print ("This is print str", file = sys. stderr) is used.
Solution:
Locate row 631 and modify the print statement. It turns out to be print> sys. stderr, '\ nTime Elapsed: % s' % (self. stopTime-self.startTime), can be changed to print ('\ nTime Elapsed: % s' % (self. stopTime-self.startTime), file = sys. stderr)
Question 6: TypeError: 'str' does not support the buffer interface
Cause: locate the problem. The problem lies in row 118. Here, s is the str type. We need to convert the passed s to the bytes type.
Solution:
Locate row 118 and change self. fp. write (s) to self. fp. write (bytes (s, 'utf-8.
2. Save
Save the changes to HTMLTestRunner. py.
3. Modify the Call Statement
In python3, open the file and use open instead of file.
Replace fp = file (filename, 'wb ') with fp = open (filename, 'wb ');
Close ()
Note: after the modification, Chinese characters will not be garbled.
Instructions for using HTMLTestRunner. py
1. Problem: during the execution of the test case, nothing will be printed. As a result, when the last restroom or the next day comes back, there is no idea where to execute or how many test cases to execute.
Solution: print the name of a test case every time you execute a test case.
Solution: When HTMLTestRunner is called, verbosity is defined as an integer greater than 1, for example, 2:
Runner = HTMLTestRunner. HTMLTestRunner (fp, title = "xxxx", description = "xxx", verbosity = 2)
The following output is displayed on the console for each execution of a use case:
E test (testcases.login.testcase1.MyTest)ok test (testcases.login.testcase2.MyTest)F test (testcases.login.testcase3.MyTest)
2. Read the HTMLTestRunner script.
Class OutputRedirector (object) # redirect the output class Template_mixin (object) # define the template required to generate the HTML result file. # If you want to change the HTML format, you can modify class _ TestResult (TestResult) # define the class inherited from unittest. TestResult. # Here we overwrite multiple methods of unittest. TestResult, such as startTest (self, test) and other class HTMLTestRunner (Template_mixin) # Here we can say it is the entry to use HTMLTestRunner. py. Defines multiple methods we can see, such as run (self, test) class TestProgram (unittest. TestProgram) # Here inherited from the unittest. TestProgram class, override the runTests method. # Command line testing
3. disadvantage: when using HTMLTestRunner to execute a test case, if the execution is interrupted in the middle, the results of the executed test case will not be printed to the html file.
Currently, I write scripts to generate html instead of HTMLTestRunner. I feel that HTMLTestRunner should not write the result into the html result file every time a use case is executed.
Test Case:
1. log on to Baidu cloud
2. Go to the "register Baidu account now" Page
3. Go to the "member center" Page
4. The name of the generated test report is 2015-01-02result.html.
Test script:
# Coding = UTF-8 # prevent Chinese garbled characters from selenium import webdriverfrom selenium. webdriver. common. by import By # load the module used by the keyboard from selenium. webdriver. common. keys import Keysfrom selenium. webdriver. support. ui import Selectfrom selenium. common. exceptions import NoSuchElementException # Load unittest module import unittest import timeimport re # Load HTMLTestRunner to generate HTMLreusltimport HTMLTestRunnerclass BaiduYun (unittest. testCase): def SetUp (self): self. browser = webdriver. chrome () self. browser. implicitly_wait (30) self. base_url = "http://yun.baidu.com" self. verficationErrors = [] self. accept_next_alert = True def Login (self): browser = self. browser. get (self. base_url + '/') u "Baidu cloud login" browser. find_element_by_name ("userName "). clear () username = browser. find_element_by_name ("userName") username. send_keys ("alu ***") username. send_keys (Ke Ys. TAB) time. sleep (2) password = browser. find_element_by_name ("password") password. send_keys ("***") password. send_keys (Keys. ENTER) time. sleep (3) browser. close () def Register (self): browser = self. browser. get (self. base_url + '/') u "register Baidu account now" "browser. find_element_by_class_name ("link-create "). click () time. sleep (2) browser. close () def Link (self): browser = self. browser. get (self. base_url + '/ ') U "member center" "browser. find_element_by_link_text ("member center "). click () time. sleep (2) browser. close () def tearDown (self): self. browser. quit () self. assertEqual ([], self. verficationErrors) if _ name __= = "_ main _": # unittest. main () testunit = unittest. testSuite () # Add the test case to the test container testunit. addTest (BaiduYun ("Login") testunit. addTest (BaiduYun ("Register") testunit. addTest (BaiduYun ("Link") # obtain the current time to facilitate the following usage. Now = time. strftime ("% Y-% m-% M-% H _ % M _ % S", time. localtime (time. time () # open a file and write the result to this file. fp = open ("result" + now + ". html ", 'wb ') runner = HTMLTestRunner. HTMLTestRunner (stream = fp, title = 'test result', description = u'result: ') runner. run (testunit) fp. close ()
F5, run, and the test result is obtained.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.