Htmltestrunner modified to Python3 version

Source: Internet
Author: User

When replying to the selenium2+python2.7 of the god of the worm, it was found that the Htmltestrunner module used to generate Htmltestrunner's test report was Python2 syntax. And I am more accustomed to Python3. And I also use the Python3.4 environment, in the Internet to find a lot of information, modified the next htmltestrunner.py

Reference: http://bbs.chinaunix.net/thread-4154743-1-1.html

: http://tungwaiyip.info/software/HTMLTestRunner.html

Modified: Http://pan.baidu.com/s/1tp3Ts?

To Modify a rollup:

Line 94th, change import Stringio to import IO

Line No. 539, change self.outputbuffer = Stringio.stringio () to Self.outputbuffer = Io. Stringio ()

Line No. 642, if not Rmap.has_key (CLS): modified to If not CLS in Rmap:

Line No. 766, change uo = O.decode (' latin-1 ') to UO = E

Line No. 775, change UE = E.decode (' latin-1 ') to UE = E

Line No. 631, change print >> sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime) to print (Sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime))

Use Htmltestrunner under Python3.4, start, introduce Htmltestrunner module error.

In the Htmltestrunner 94 line, is the use of the Stringio, but Python3, there is no stringio. Instead, it's IO. Stringio. So change this line to import IO

In 539 rows of htmltestrunner, Self.outputbuffer = Stringio.stringio () is modified to Self.outputbuffer = IO. Stringio ()

After the modification, the module was successfully introduced.

Execute script code:

#-*-coding:utf-8-*-#the packages needed to introduce Webdriver and UnitTest fromSeleniumImportWebdriver fromSelenium.webdriver.common.byImport by fromSelenium.webdriver.common.keysImportKeys fromSelenium.webdriver.support.uiImportSelect fromSelenium.common.exceptionsImportnosuchelementexception fromSelenium.common.exceptionsImportnoalertpresentexceptionImportUnitTest, time, re#Introducing the Htmltestrunner packageImportHtmltestrunnerclassBaidu (unittest. TestCase):#Initialize Settings    defsetUp (self): Self.driver=Webdriver. Firefox () self.driver.implicitly_wait (30) Self.base_url="http://www.baidu.com/"self.verificationerrors=[] Self.accept_next_alert=True#Baidu Search use case    defTest_baidu (self): driver=self.driver driver.get (self.base_url) driver.find_element_by_id ("kw"). Click () driver.find_element_by_id ("kw"). Clear () driver.find_element_by_id ("kw"). Send_keys ("Selenium Webdriver") driver.find_element_by_id ("su"). Click () time.sleep (2) Driver.close ()defTearDown (self): Self.driver.quit () self.assertequal ([], self.verificationerrors)if __name__=="__main__":    #define a Test ContainerTest =UnitTest. TestSuite ()#Adding test cases to the test ContainerTest.addtest (Baidu ("Test_baidu"))    #define a path for report storage, supporting relative pathsFile_path ="f:\\robottest\\result.html"File_result= Open (File_path,'WB')    #Defining test ReportsRunner = Htmltestrunner.htmltestrunner (stream = file_result, title = u"Baidu Search test Report", Description = U"use case Execution")    #Run test Casesrunner.run (Test) file_result.close ()

After running the test script, the error is found:

File "C:\Python34\lib\HTMLTestRunner.py", line 642, in Sortresult

If not Rmap.has_key (CLS):

So go to line 642 to modify the code:

Continue to error after running:

Attributeerror: ' str ' object has no attribute ' decode '

Go to 766, 772 lines continue to modify (note: 766 lines is UO and 772 lines is the UE, then blind, did not notice these, thought is the same, leading to some inexplicable errors, tossing half a day):

Modified to run, found and error:

File "C:\Python34\lib\HTMLTestRunner.py", line 631, in run

Print >> sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime)

typeerror:unsupported operand type (s) for >>: ' Builtin_function_or_method ' and ' _io. Textiowrapper '

Go to 631 to view and discover the only print in the entire program:

Print >> sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime

This is the 2.x notation, we modify the print to 3.x, the following changes:

Print (Sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime))

Continue running scripts, OK runs successfully

Viewing the specified directory generates the result.html

Click to open the report:

Htmltestrunner modified to Python3 version

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.