current position:Home>Regular re parameter replacement of Python 3 interface automation test framework

Regular re parameter replacement of Python 3 interface automation test framework

2022-01-29 18:06:36 Mr. forgets his name

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

re Regular expressions

 here 【 Not detailed 】 Introduce the usage of regular expressions and other methods , Baidu has a pile of perfect interpretations re The blog of !
 Copy code 

Application in interface test

 In the process of implementing interface automation test framework coding , Use ddt Design test cases , You need to parameterize ;
 So we use re Regular expressions to match the request parameters that need to be parameterized , Then replace with new request parameters ;
 Copy code 
  • Presentation parameters :data={"phone":"#phone#","passwd":"#pwd#","name":"#name#","amount":123}
  • Regular expressions :parttern=r"#(.+?)#"
^: Match the beginning of the string 
$: Match the end of the string 
.: Match any character , Except for line breaks 
+: matching 1 One or more expressions 
?: matching 0 Or 1 Fragments defined by the previous regular expression , Not greedy ,( Stop searching when the left and right boundaries arrive )
 Copy code 
  • re.match() and re.search()、re.findall() The difference between :
re.match Match only the beginning of the string , If the string doesn't start with a regular expression , The match fails , The function returns None; and re.search Match the entire string , Until we find a match .
 Be careful : match  and  search  It's a match , findall  Match all .
 Copy code 
  • group() and groups() The difference between
 The former matches the whole string ,group()  You can enter more than one group number at a time , In this case it will return a tuple containing the values corresponding to those groups .
 for example :ress.group(0),ress.group(1),ress.group(0,1)  result :#phone# 、phone 、'#phone#', 'phone')
 The latter returns a tuple containing all the group strings , Match the found string 
 Copy code 

encapsulation re Code display

import re
from common.HandleConfig import conf
from common.HandleLogging import log


class TestData:
    """ What this class does : It is specially used to save some data to be replaced """
    pass
 
 
def parameters_handle(data,partten=r"#(.+?)#"):
    ''' Parameterization according to regular matching , Parameters to be replaced '''
    
    #  Determine if there is any data that needs to be replaced 
    while re.search(partten, data):
        #  Match the first data to be replaced 
        res = re.search(partten, data)
        #  Extract the content to be replaced 
        item = res.group()
        #  Get the data item in the replacement content 
        key = res.group(1)
        try:
            #  Find the corresponding content in the configuration file according to the data item in the replacement content , Replace 
            data = data.replace(item, conf.get("test_data", key))
        except:
            try:
                data = data.replace(item, getattr(TestData,key))# If there is no such attribute in the class , An error will still be reported, indicating that there is no attribute of this class in the class 
            except:
                log.error("TestData Not in class {} Class properties ".format(key))
                break
    #  Return replaced data 
    return data
 Copy code 

copyright notice
author[Mr. forgets his name],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201291806338556.html

Random recommended