A. A simple example of complete

# Import 
Import Requests
 Import the unittest
 Import Time
 Import HTMLTestRunner
 Import OS
 Import smtplib
 from email.mime.text Import MimeText
 from email.mime.multipart Import MimeMultipart 

# -defined test class 
class Test_lazy1 (of unittest.TestCase): 

    # with initialization operation performed before Example: 
    DEF the setUp (Self):
         # custom url 
        self.r_url = " http://sztm.mting.info/yyting/activity/myGroupPurchaseList.action " 
        #定制request heasders
        self.r_header ={"User-Agent":"Android6.0.1/yytingwap/vivo/vivo Y66/ch_beta/202/Android",
                    "Connection": "keep-alive",
                    "Host": "sztm.mting.info"
                  }
        #定制请求参数
        self.r_params={"imei":"ODY2Njk3MDM3MTYyMzgx",
                 "token":" A9xJ48T0X9fP1SZKF6GjFg k6gMl9_1fXI2j67rO62pudaJ15ZoGqH- ** _ " ,
                  " optype " : " H " ,
                  " referId " : "" ,
                  " size " : 20 is 
                 } 

        # transmission request, and stores the response r in 
        self.r = requests.get (url = self.r_url, headers = self.r_header, params = self.r_params) 

        self.re_dict = self.r.json () # parse the json response data (converted to the dictionary, but also encountered as a list ----- --- to be studied) 
        self.status self.re_dict = [ "status"] # Fight group status list values 
        self.msg self.re_dict = [ " msg " ] # give msg value 
        self.book_list self.re_dict = [ " List " ] # give fight resource group information list 


     # determines whether the status code 200 is 
    DEF test_status_code (Self): 
        self.assertEqual (self.r.status_code, 200, MSG = " return a status code other than 200 " ) 

    # Analyzing I fight groups status returned list 
    DEF test_status (Self): 
        self.assertEqual (self.status, 0, msg = " Status is not zero, return to the list of failure " ) 

    @ unittest.skip ( "This entry does not carry a use case " )
     # judge message should be empty 
    DEF test_msg (Self): 
        self.assertIsNone (self.msg, msg = " msg is not null " ) 

    # judge just fight Mission success is in the list of books (without this operation the above code, for example assume that we have just had to fight group) 
    DEF test_bookisin (Self):
         # define an empty list, storage resource ID 
        self.entity_list = [] 
        self.key_name = " entityId " 
        # traversal resource dictionary, the resulting resource id stored in the list 
        for info in self.book_list:
             for k in info:
                 IF k ==self.key_name: 
                    self.entity_list.append (info [K]) 

        self.assertIn ( 37301, self.entity_list, MSG = " ID 37301 resource group list is not to fight " ) 

    # Test rehabilitation 
    DEF the tearDown (Self):
         Print ( " test completed " ) 



# define a method, take the latest test reports 
DEF get_testresult (result_dir):
     # list all files in the directory result_dir, results are returned in a list. 
    = lists os.listdir (result_dir)
     # the Sort to sort by keyword key, the element of the lambda parameter fn as a list of lists, acquired last modified file 
    # Finally, lists of elements, by file modification time from small to large size . 
    lists.sort (Key = the lambdaFn: os.path.getmtime (result_dir + ' \\ ' + Fn))
     # obtain the latest file absolute path 
    file_path = the os.path.join (result_dir, Lists [-1 ])
     return file_path 


# define the message transmission method 
DEF the send_mail (new_result): 
    f = open (new_result, " rb " ) # open file 
    mail_body = f.read () # read the file contents 
    f.close () 

    # enter the Email address and password: 
    from_addr = " [email protected] " 
    password = " ftuttfiwumrgbaca "   #
    # Enter the recipient's address: 
    to_addr = " [email protected] " 
    # Enter the SMTP server address: 
    smtp_server = " smtp.qq.com " 

    msg = MimeMultipart ( " Mixed " ) 
    msg [ " Subject " ] = " lazy listen book test report " 
    msg [ " the From " ] = " lazyman " 

    # construct the message body 
    mail_text = MimeText ( " lazy listen books latest test reports " ) 
    msg.attach(mail_text)

    mail_html=MIMEText(mail_body," HTML " , " UTF_8 " ) 
    mail_html [ " the Content-Disposition " ] = ' Attachment; filename = "testresult.html" ' 
    msg.attach (mail_html) 

    Server = smtplib.SMTP (smtp_server, 587)   # 465/587 
    Server. set_debuglevel (1 ) 
    server.starttls () 
    server.login (from_addr, password) 
    server.sendmail (from_addr, to_addr, msg.as_string ()) 
    server.quit () 


# # definition of html files, used to store test report 
# gets the current time 
now = The time.strftime ( " % D%% Y-M-% of M_% S% H_" , Time.localtime ())   # first time format parameter, the second parameter is obtained in the form of a current time struct_time 
# storage path and file name of the test report, the file name is added to the current time to generate each different the test report 
filename = " F: \\ Python TestResults \\ \\ \\ lianxiProject Case " + now + " _result.html " 
# invasive test report html file, then still an empty file 
fp = Open (filename, " wb " ) 



# query for all test cases under the target directory 
test_dir = " F: Python \\ \\ \\ lianxiProject Case3 "   # use case catalog 
the Discover = unittest.defaultTestLoader.discover (test_dir, pattern = " . * _ the test Py ") 

# Execution examples and test report generation
= HTMLTestRunner.HTMLTestRunner Runner (fp = stream, title = U " test report " , the Description = U " Report " ) # define HTMLTestRunner test reports, documents stream custom report written; title as the title of the report; description for the report and describes 
runner.run (Discover) # reported cases with the operation of the test vessel, and write the results in 
fp.close () # Close the file stream 


result_dir = " F.: lianxiProject \\ \\ \\ TestResults Python " # to find the definition of test reports directory 
new_result = get_testresult (result_dir) # find the latest test reports 
send_mail (new_result) # send e-mail

The basic idea of ​​the outline.

 

Guess you like

Origin www.cnblogs.com/youzaijiang/p/11528147.html