当前位置:网站首页>Interface joint debugging test script optimization v4.0

Interface joint debugging test script optimization v4.0

2022-07-06 23:46:00 Night * star

introduction : Next chapter Interface joint commissioning test script optimization v2.0, This chapter optimizes script parameters , Put the parameters into the file , After the implementation script is compiled once , Later, you only need to maintain files .

Catalog

Optimization task :

Problems encountered :

Script implementation :

Summary :

advantage :


Optimization task :

Put the relevant interface test parameters into csv In file , Read the relevant parameters through the program , Pass it into the interface test script .

Problems encountered :

Create a csv file , Write the interface parameters into the file ,csv The documents are as follows :

  1.   For common parameters url、expected_result、interfacename Just call it directly .
  2. But because each interface has different parameters , The number of parameters is also different , You need to use the loop again to read .
    1. Here, the number of parameters is written directly in front of the parameters , Then define a j The variable is equal to the number of parameters .
    2. It's not hard to find out ,H Column starts as a loop index The value is 7, And the end value of the loop is 2*j+7.
    3. So the following different parameter data can be read in the loop , The code reference is as follows .

Script implementation :

#  Use parameterization technology to optimize the test script of joint debugging of multiple interfaces 
import requests
import csv


class Workflow_Test_v4():
    def userinterface_test(self, url, userinfo, expected_result, interfacename):
        response = requests.post(url, data=userinfo).text
        print(response)
        r = response.find(expected_result)
        if r > 0:
            print(interfacename, " The test passed ")
        else:
            print(interfacename, " Test to fail ")


if __name__ == '__main__':
    workflowobj4 = Workflow_Test_v4()
    file = open("test1.csv", "r")
    table = csv.reader(file)
    for row in table:
        #  Read relevant contents line by line , And set it as the corresponding parameter 
        url = row[1]
        expected_result = row[3]
        interfacename = row[5]
        # print("url yes :", url, "  ", "expected_result yes :", expected_result, "  ","interfacename yes :", interfacename)
        #  Read interface test data 
        userinfo = {}
        j = int(row[6])
        for i in range(7, 2*j+7, 2):
            userinfo[row[i]] = row[i+1]
        # print(userinfo)
        #  Instantiate the test class , Incoming test data 
        workflowobj4.userinterface_test(url, userinfo, expected_result, interfacename)

Summary :

  • The problem of script parameterization is mainly to find the rules of parameters
  • Common parameters are :
    1. url: Interface access address
    2. expected_result: The interface responds to the expected results
    3. interfacename: The name of the interface
  • Parameters of personality : Interface test data is different
    1. Manually count the number of test data , Write it into the test data template
    2. Circulation mode : Find the law of circulation , Just analyze a few values
      1. Initial value ----index = 7
      2. Termination value ----index = 2*j+7( because python Can't get the last place , So take it as the end value of the range )
      3. Change a few at a time ---- Because parameters have two values: key value and attribute , So when taking down a parameter , want i+2.
    3. Save the found parameters in the dictionary : Dictionary name [key] = value namely userinfo[row[i]] = row[i+1]

advantage :

Future interface tests , You only need to maintain the document of interface test data , There is no need to modify any code .

原网站

版权声明
本文为[Night * star]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131031222346.html