Hematemesis finishing, interface automation testing-interface dependency/upload interface processing (project instance)


foreword

Two common interface dependency processing methods

1. The fields of the request body depend on
this situation. Most of the cases are in the currently tested interface, and the fields in the request body of its pre-interface should be used in the current interface request body to continue to use.

For example, the interface for modifying user information will use the field of user name, which is passed in the request body when creating the user. We parameterize the request body of the interface for creating the user so that it can dynamically generate the user name .

Because it changes every time, when the interface for modifying user information is executed, if we still use the parameterization of the previous interface, it will inevitably cause the user names of the two to be different, which will cause the interface to fail to execute, so we To perform interface dependency processing on this type of interface

B1

We handle interface dependencies directly in the code of the test class:

if case["check_info"] == 'user_info':
	user_name = json.loads(new_data)['name']
	case_logger.info("{:-^50s}".format(f"user_name:{
      
      user_name}"))
	setattr(do_re, 'user_name', user_name)

2. Response body field dependency
The method of using the response body field in the next interface is still very common in interface dependency. Often the previous interface is a get-type query interface, and we get it from the response body of the interface Take the corresponding field and use it in the request body of the next interface.

B2

We handle interface dependencies directly in the code of the test class:

if case["check_info"] == 'get_op_svc_servicestage_domainId':
	op_svc_servicestage_domainId = res.json()['user']['domain']['id']
	case_logger.info("{:-^50s}".format(f"op_svc_servicestage_domainId:{
      
      op_svc_servicestage_domainId}"))
	setattr(do_re, 'op_svc_servicestage_domainId', op_svc_servicestage_domainId)

Handle the type interface that needs to upload files

1. Interface analysis
Here, upload directly on the front end, capture the package through the F12 development tool of the Chrome browser, and you can see the request and corresponding information of the upload file interface, because it is the company's project interface, it will do some mosaic processing:

B3

Look at the request parameters again:

B4

We can see from the request that the request method is multipart/form-data form format, and notice that although the file is uploaded, the file path and file name are not displayed in the request parameters, and the fileList value is "binary", then You can know the byte content of the uploaded file

2. Postman example
Take postman as an example, select the form-data option for body, fill in fileList[] for key and select the format as file, select a local file in value to upload, and then initiate a request:

B5

3. Requests example
Just now we implemented the request to upload files with the help of tools, and postman can convert our request into python code format.

The specific steps are as follows:
first use postman to build the parameters and debug successfully;
click the Code below the Save button;

B6

Copy the code after selecting the language as Python - Requests

B7

Paste the copied code into the development tool of Pycharm or VS code and run it

import os
import requests

from Commons.constants import UPLOAD_DIR

url = "https://接口地址/1.0/product/style-gallery"

payload = {
    
    }
files = [
    ('fileList[]', open(os.path.join(UPLOAD_DIR, 'stylegallery.png'), 'rb'))
]
headers = {
    
    }

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text.encode('utf8'))

So far, the processing of the file upload interface has been completed, and subsequent testing or interface automation can be performed on this basis for secondary development.

4. Solution 2
If the above method cannot solve the problem, then use the second method, basically there will be no problem.

Let's take a look at the request content for uploading files:

B9

Next, apply the following template to the value of the request parameter files of the requests library:

files = {
    
    
            '${name}': ('${filename}', open( '${filepath}', 'rb'), '${Content-Type}')
        }

${name}: the value of name "fileList[0]"
${filename}: the value of filename "a.jpg"
${filepath}: the file path of filename
${Content-Type}: the content-type The value "image/jpeg"
replaces the variable corresponding to the template with the actual value, resulting in:

files = {
    
    
            'fileList[0]': ('a.jpg', open(os.path.join(UPLOAD_DIR, 'a.jpg'), 'rb'), 'image/jpeg')
        }

Note: To confirm whether the file path is correct, I use a relative path here, and then pass the files to the corresponding method of requests.

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

How can you see a rainbow without experiencing wind and rain; how can you become a master without tempering yourself. Adhere to the dream, pursue excellence, and the pace of struggle will never stop. Before every dawn, there is glory waiting.

Struggle is like a whetstone, sharpening the edge; hard work is like sunshine, illuminating the way forward. Not afraid of challenges and pursuing excellence, only by persistently striving can we surpass ourselves, bloom the brilliance of life, and let dreams shine endlessly in the journey of struggle!

On the journey of struggle, don't be afraid of failure, because every fall is an opportunity to stand up stronger; don't pursue perfection, because growth is accumulated through repeated attempts. As long as you have courage and persistence, you will firmly move towards your dream.

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/132300641