当前位置:网站首页>Interface joint commissioning test script optimization V5.0 (end)

Interface joint commissioning test script optimization V5.0 (end)

2022-07-05 04:34:00 Night * star

introduction : This chapter will be based on v4.0, Prepare interface test report , At the same time, summarize the previous optimization stages .


Directory of interface joint commissioning test optimization stage :


This time v5.0 List of articles

V5.0 Optimization task

Design thinking

Finally, the complete script implementation

advantage :


V5.0 Optimization task

Write test report

Design thinking

  1. The actual response results and test conclusions of the interface need to be stored in the test report .
  2. The test report should include : The name of the interface , The actual return value of the interface , Interface test conclusion .
  3. You can define a result_data The dictionary of is used to store the actual return value of the interface and the test conclusion , Last need return After obtaining data result_data
  4. Define a new method to write the dictionary data content into the test report .
    1. Because the dictionary contains keys + value , So when writing content circularly, use :for key, value in result_data.items():
    2.     #  Write relevant test conclusions into the test report 
          def result_report(self, interfacename, reportfilename, result_data):
              #  This time it is written in the form of addition , use a( Of course w Overwriting is also possible )
              file = open(reportfilename, "a")
              for key, value in result_data.items():
                  file.write(interfacename + "," + key + "," + value + ",")
              #  Write a line of test conclusion and then wrap 
              file.write("\n")
              #  Remember to close the file after writing 
              file.close()

Finally, the complete script implementation

#  Optimize the test script structure , Script parameterization and test report all technologies are combined 

import requests
import csv


#  Define test classes 
class Workflow_Test_v6():
    #  Interface test general script 
    def userinterface_test(self, url, userinfo, expected_result, interfacename):
        #  Define dictionary data , Store the actual interface response results and test conclusions 
        result_data = {}
        #  Send the request and get the response 
        response = requests.post(url, data=userinfo).text
        # print(response)
        #  Store the actual return value of the interface into the dictionary 
        result_data[" The actual return value of the interface "] = str(response)
        r = response.find(expected_result)
        if r > 0:
            print(interfacename, " The test passed ")
            #  Store the test conclusion in the dictionary 
            result_data[" Test conclusion "] = " The test passed "
        else:
            print(interfacename, " Test to fail ")
            result_data[" Test conclusion "] = " Test to fail "
        print(result_data)
        return result_data

    #  Write relevant test conclusions into the test report 
    def result_report(self, interfacename, reportfilename, result_data):
        #  This time it is written in the form of addition , use a( Of course w Overwriting is also possible )
        file = open(reportfilename, "a")
        for key, value in result_data.items():
            file.write(interfacename + "," + key + "," + value + ",")
        #  Write a line of test conclusion and then wrap 
        file.write("\n")
        #  Remember to close the file after writing 
        file.close()


if __name__ == '__main__':
    workflowobj6 = Workflow_Test_v6()
    file = open("test1.csv", "r")
    reportfilename = "test_result6.csv"
    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 = {}
        result_data = {}
        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 
        result_data = workflowobj6.userinterface_test(url, userinfo, expected_result, interfacename)
        #  Call write test report file , Incoming test conclusion and actual interface response result 
        workflowobj6.result_report(interfacename, reportfilename, result_data)

Be careful : Every time open csv Remember to close the file after viewing it , Otherwise, if the document is open , The program won't run .

advantage :

  1. A scripted program can correspond to several interface tests
  2. You only need to maintain the test data to complete the test of any group of interfaces
  3. All interface test conclusions can automatically generate test report files .
原网站

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