How to generate Htmltestrunner reports

Source: Internet
Author: User

Today, I learned how to generate a Htmltestrunner test report.  
Next article, for the UnitTest framework, after running, the test results are not easy to see, and when multiple cases exist, the case result record may be incorrect.  
For this reason, htmltestrunner.py is introduced as an extension of the Python standard library unittest module. It can generate an intuitive HTML test report.  
First, download the htmltestruner.py file.  
Source address: http://tungwaiyip.info/software/HTMLTestRunner.html 
This is for the Python2.7 version, so for python3.x use, Need to change a few places. And thank http://www.bubuko.com/infodetail-529431.html for sharing.  
specific changes are as follows:  
Line 94th, change import Stringio to import io 
Line No. 539, Self.outputbuffer = Stringio.stringio () modified to self.outputbuffer= IO. Stringio ()  
Line No. 631, change print >> sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime) to print ( Sys.stderr, ' \ntimeelapsed:%s '% (self.stoptime-self.starttime))  
Line No. 642, if not Rmap.has_key (CLS): Modify to If Notcls in rmap: 
Line No. 766, modify UO = O.decode (' latin-1 ') to UO = e 
Line No. 775, change UE = E.decode (' latin-1 ') to UE = E &NBSP
Line No. 778, output = Saxutils.escape (Uo+ue), modified to output = Saxutils.escape (str (UO) +STR (UE)),

The above changes, saved successfully, and then put htmltestrunner.py into the C:\Python34\Lib directory, check whether the load is successful, in Python IDLE input
Import Htmltestrunner
If there is no error, then the load is successful.

Test Case:
1. Login to Baidu Cloud
2. Enter the "Register now Baidu Account" page
3. Go to the "Member Center" page
4. The file name to generate the test report is: 2015-01-02result.html.

Test script:

#coding =utf-8 #防止中文乱码From seleniumImport WebdriverFrom selenium.webdriver.common.byImport by#加载键盘使用的模块From Selenium.webdriver.common.keysImport KeysFrom Selenium.webdriver.support.uiImport SelectFrom Selenium.common.exceptionsImport Nosuchelementexception#加载unittest模块Import UnitTestImport timeImport re#加载HTMLTestRunner, used to generate HTMLREUSLTImport HtmltestrunnerClassBaiduyun(UnitTest. TestCase):DefSetUp(self): Self.browser=webdriver. Chrome () self.browser.implicitly_wait (self.base_url=)"Http://yun.baidu.com" self.verficationerrors=[] self.accept_next_alert=TrueDefLogin(self): Browser=self.browser 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 (Keys.tab) Time.sleep (2) Password=browser.find_element_by_name ("Password") Password.send_keys ("* * *") Password.send_keys (keys.enter) Time.sleep (3) Browser.close ()DefRegister(self): Browser=self.browser browser.get (self.base_url+‘/‘)U "" "Register now Baidu Account" "" Browser.find_element_by_class_name ("Link-create"). Click () Time.sleep (2) Browser.close ()DefLink(self): Browser=self.browser browser.get (self.base_url+‘/‘)U "" "Member Center" "" Browser.find_element_by_link_text ("Member Center"). Click () Time.sleep (2) Browser.close ()DefTearDown(self): Self.browser.quit () self.assertequal ([],self.verficationerrors)if __name__== "__main__": # Unittest.main () testunit=unittest. TestSuite ()  #将测试用例加入到测试容器中 testunit.addtest (Baiduyun ( "Login ")) Testunit.addtest (Baiduyun (" Register ")) Testunit.addtest (Baiduyun (  #获取当前时间, this facilitates the use of the following. now = Time.strftime ( "%y-%m-%m-%h_%m_%s", Time.localtime (Time.time ()))  #打开一个文件, write 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 ()               
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • 48
    • all
    • /
    • /
    • /
    • /li>
    • ,
    • ,
    • ,
    • up-
    • -
    • +
    • -
    • +
    • *
    • +
    • ,
    • ,
    • +
    • $
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

F5, run, and get the test result.
In Python3.4, you can get:

In the C:\Python34\ directory, there are files

When opened,:

This looks more intuitive.
At the same time, if there is an incorrect case, it will only show the fail case (the result of the other test), and the pass is still shown for the pass:

Analysis:
1. For fp=file (filename, "WB") in the python3.x file is no longer used, you need to use the Fp=open (filename, "WB")
Also be sure to remember that the close FP is Fp.close ()
2. In order to set the name of result file to be more intuitive, you need to get the current time, resolved.
Now=time.strftime ("%y-%m-%m-%h_%m_%s", Time.localtime (Time.time ()))

So, the problem is, though, the resulting result is intuitive, but for a script with multiple case, so for the maintenance and use of the script is still inconvenient, this need to consider the integration case script, you need to learn the test suite.

How to generate Htmltestrunner reports

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.