python+jinja2 Tool for Batch Generation of Interface Data

Keywords: Python

When we do interface testing, we often encounter a situation that we need to check all kinds of possible parameters of the interface. Manual modification is very troublesome, especially those interface parameters have dozens or more. Is there a way to generate and process the specified parameters in batches?

The answer is yes!

python's jinja2 template library can satisfy our needs very well. By maintaining a raw data template and template the variables we want to generate dynamically, we can achieve the requirements.

Now we have such a request data

{
    "abc":"123",
    "p2p":"123",
    "smid":"20180807220733939b66d80092eea34ce9e77f30bedff12345b7d5a3faa11b",
    "test":{
        "test1":"1",
        "test2":"2"
    },
    "test3":"3"
}

If you want to batch modify the smid field and generate new request data, you can do the following:

1. First, a new text file named fp_template.txt is created.

2. Copy and paste the above interface request data into the txt file, which we use as a "template file";

3. Create a new predata folder to store the generated data files.

4. Template the smid field (the template format can refer to jinja2's grammar, not to be redundant here), so the above request parameters become the following:

{
    "abc":"123",
    "p2p":"123",
    "smid":"{{ smid }}",
    "test":{
        "test1":"1",
        "test2":"2"
    },
    "test3":"3"
}

The implementation code is as follows:

# -*- coding: UTF-8 -*-
from jinja2 import Environment,FileSystemLoader
import os

class DataTemplateFaker:
    def __init__(self):
        self.aesPath = os.getcwd()#Get the startup path
        self.resultPath = self.aesPath + "/predata/"#Specify the path to save the generated data
        self.templateFile = "fp_template.txt"

    #Modify the format of smid we want to generate in batches
    def init_smid(self,start,end):
        smidArg = [x for x in range(start, end)]
        re = []
        for n in smidArg:
            re.append("20180807220733939b66d80092eea34ce9e77f30bedff" + str(n) + "b7d5a3faa11b")
        return re

    #Operating Template File
    def preContent(self,arg):
        env = Environment(loader=FileSystemLoader('./'))
        tpl = env.get_template(self.templateFile)
        renderContent = tpl.render(smid=arg)
        return renderContent

    #Replace template files in batches by modifying smid lists and write them to specified files
    def makeContent(self,preList):
        x = 0
        for i in preList:
            x = x + 1
            filename = str(self.resultPath) + 'data_' + str(x) + '.txt' #Used to distinguish between storing newly generated request data (which can also be written to a file)
            renderContent = self.preContent(i)
            with open(filename, 'w') as f:
                f.writelines(renderContent)
                f.close()

if __name__ == "__main__":
    AT = DataTemplateFaker()
    reList = AT.init_smid(1,10)#Control the scope of generated data
    AT.makeContent(reList)

By running the program, you can get the newly generated data.

Of course, we can also specify other parameters to modify, such as modifying p2p, only need to modify the template file:

{
    "abc":"123",
    "p2p":"{{ p2p }}",
    "smid":"20180807220733939b66d80092eea34ce9e77f30bedff12345b7d5a3faa11b",
    "test":{
        "test1":"1",
        "test2":"2"
    },
    "test3":"3"
}

Then add a method init_p2p() to the code.

# -*- coding: UTF-8 -*-
from jinja2 import Environment,FileSystemLoader
import os

class DataTemplateFaker:
    def __init__(self):
        self.aesPath = os.getcwd()#Get the startup path
        self.resultPath = self.aesPath + "/predata/"#Specify the path to save the generated data
        self.templateFile = "fp_template.txt"

    #Modify the format of smid we want to generate in batches
    def init_smid(self,start,end):
        smidArg = [x for x in range(start, end)]
        re = []
        for n in smidArg:
            re.append("20180807220733939b66d80092eea34ce9e77f30bedff" + str(n) + "b7d5a3faa11b")
        return re
    #Modify the format of p2p we want to generate in batches   
    def init_p2p(self,start,end):
        p2pArg = [x for x in range(start, end)]
        return p2pArg

    #Operating Template File
    def preContent(self,arg):
        env = Environment(loader=FileSystemLoader('./'))
        tpl = env.get_template(self.templateFile)
        renderContent = tpl.render(smid=arg)
        return renderContent

    #Replace template files in batches by modifying smid lists and write them to specified files
    def makeContent(self,preList):
        x = 0
        for i in preList:
            x = x + 1
            filename = str(self.resultPath) + 'data_' + str(x) + '.txt' #Used to distinguish between storing newly generated request data (which can also be written to a file)
            renderContent = self.preContent(i)
            with open(filename, 'w') as f:
                f.writelines(renderContent)
                f.close()

if __name__ == "__main__":
    AT = DataTemplateFaker()
    reList = AT.init_p2p(1,10)#Control the scope of generated data
    AT.makeContent(reList)

This is only a simple demo, of course, there are many other optimization areas, such as multi-field modification, the introduction of faker library for association to generate forged data, the more complex and parameters of the interface the more applicable, the other methods can be overwhelming, Renjun play.

Posted by hastishah on Tue, 27 Aug 2019 08:49:50 -0700