Python3 Modify the Htmltestrunner, generate a test report with screenshots, and send a test message (ii)

Source: Internet
Author: User
Tags assert throw exception wrapper

3. How to enter the address and name of the first step into the table in the second step ...

Using the method of finding elements above, it is found that Report_test_with_output_tmpl is used to output test results in htmltestrunner.py. We just need to write the URL and name in it.

Assuming that we can now navigate to the specifics of each use case and define the URL as a variable html, the name is defined as the variable name, and the code to modify the htmltestrunner.py is as follows:

Report_test_with_output_tmpl = R"""<tr id= '% (tid) s ' class= '% (Class) s ' > <td class= '% (style) s ' ><div class= ' testcase ' >% (DESC) s</ div></td> <td colspan= ' 5 ' align= ' center ' > <!--css div popup start--> <a class= "Popup_link "Onfocus= ' This.blur (); ' href=" Javascript:showtestdetail (' div_% (tid) s ') ">% (status) s</a> <td>< ; a href= "% (HTML) s" target= "_blank" >% (name) S</a></td> <!--here Modified--<div id= ' div_% (tid) s ' CLA ss= "Popup_window" > <div style= ' text-align:right; Color:red;cursor:pointer ' > <a onfocus= ' This.blur (); ' onclick= ' document.getElementById (' div_% (tid) s '). Style.display = ' None ' > [x]</a> </div> <pre>% (script) s < /pre> </div> <!--css Div popup end--> </td></tr>""" #variables: (tid, Class, style, desc, status)

Find the function defined by the variable: _generate_report_test, modified as follows:

row = Tmpl%Dict (Tid=tid, Class= (n = = 0 and 'Hiddenrow' or 'None'), Style= N = = 2 and 'Errorcase' or(n = = 1 and 'Failcase' or 'None'), desc=desc, Script=script, HTML= HTML,#Modify HereName = name,#Modify HereStatus =Self . Status[n],) rows.append (row)if  notHas_output:return

Defining variables, outputting variables in URL format has been done. Next is assigning values to variables ~ ~

Idea: According to the great Gods of the blog, see the function _generate_report_test UE and UO represent unittest in the print log and throw Exception Log. The idea is that if I print the URL and name to the log, it's easy to extract the URL and name from the UE and UO.
began to transform the UnitTest ~ ~

Idea: I want to output every use case, either fail or pass. If the use case executes correctly, only the log is printed. If the use case executes incorrectly, the log is printed and the Exception log is thrown.

UnitTest is Python3 's own library, we need to find UnitTest folder, modify the case.py. Path: XXX (own Python installation path) \python\python36\lib\unittest
Two additional methods in the TestCase class of case.py

Def screenshot ():
def add (func):
Specific as follows:

classTestCase (object):defScreenshot ():#(i) the method of the definition mentioned inImageName = str (time.time ()) +'. PNG'ImagePath='//sdcard//'+imageName Path= OS.GETCWD () +'\\screenshot'          if  notos.path.exists (path): Os.mkdir (path) Os.system ("adb shell//system//bin//screencap-p"+ImagePath) Os.system ('adb Pull'+ ImagePath +path)Print('Lustrat'+ Path +'\\'+ ImageName +'Luend')#output log, plus ' Lustrat ' luend ' special characters for easy interception    defAdd (func):#How to increase the print log        defWrapper (self, first, second, msg=None):Try: Func (Self, first, second, MSG=None) Testcase.screenshot ()exceptAssertionError:TestCase.screenshot ()RaiseAssertionerror (msg)#Throw Assertionerror        returnWrapper

Then add the @add adorner before the Assert method used. Note the transfer of the defined Func is consistent with the Assert method. If I use it.

@add #此处修改
def assertequal (self, first, second, msg=None): "" "" "Fail If the the and the objects are Unequal as determined by the ' = = ' operator . """ = Self._getassertequalityfunc (first, second) Assertion_func (first, second, msg =msg)

The htmltestrunner.py UO and UE output logs contain the address information of the print

The last step, take out the UE and UO about the URL and name, and assign a value to the variable HTML and name is done.

    def_generate_report_test (self, rows, CID, Tid, N, T, O, E):#e.g. ' pt1.1 ', ' ft1.1 ', etcHas_output = BOOL (oore) Tid= (n = = 0 and 'P' or 'F') +'t%s.%s'% (cid+1,tid+1) name= T.id (). Split ('.') [-1] Doc= T.shortdescription ()or ""desc= doc and('%s:%s'% (name, doc))orname Tmpl= Has_output andSelf. Report_test_with_output_tmplorSelf . Report_test_no_output_tmpl#O and E should be byte string because they is collected from stdout and stderr?        ifisinstance (O,STR):#todo:some problem with ' string_escape ': it escape \ n and mess up formating            #UO = Unicode (O.encode (' String_escape '))UO =eElse: UO=oifisinstance (E,STR):#todo:some problem with ' string_escape ': it escape \ n and mess up formating            #UE = Unicode (E.encode (' String_escape '))UE = O#Modify Here        Else: UE=o script= self. Report_test_output_tmpl%dict (ID=tid, Output= Saxutils.escape (str (UO) +STR (UE)), S= str (UO) +STR (UE)#change here to start        ifS.count ('PNG')!=0:#determine if there is a picture in the loghtml = S[s.find ('Lustrat') +7:s.find ('Luend')] name= Html[html.find ('shot\\') +5:]        Else: HTML=' 'name=' '              #End of modification hererow = Tmpl%Dict (Tid=tid, Class= (n = = 0 and 'Hiddenrow' or 'None'), Style= N = = 2 and 'Errorcase' or(n = = 1 and 'Failcase' or 'None'), desc=desc, Script=script, HTML=HTML, name=Name, status=Self . Status[n],) rows.append (row)if  notHas_output:return

Baidu Cloud Disk: UnitTest folder case.py Modify Link: https://pan.baidu.com/s/1eTMJu86 Password: n19o

htmltestrunner.py Link: https://pan.baidu.com/s/1dGSRbg9 Password: lw0e

Python3 Modify Htmltestrunner, generate some test reports, and send test messages (ii)

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.