Technology sharing | In interface automation testing, how to test file upload?

During the server-side automated testing process, the content-type in the request header corresponding to the file upload type interface is multipart/form-data; boundary=..., when encountering this type of interface, use Java's REST Assured or Python's Requests can be resolved.

Combat practice

Python version

In the Python version, you can use the files parameter to upload files. files requires the parameter content to be passed in dictionary format, the key value is the name of the uploaded file, and the value usually requires a binary mode file stream to be passed.

url = 'https://httpbin.ceshiren.com/post'
 files = {"hogwarts_file": open("hogwarts.txt", "rb")}
 r = requests.post(url, files=files)
 r.text
{
"args": {},
"data": "",
"files": {
"hogwarts_file": "123"
},
"form": {},
...省略...
"url": "https://httpbin.ceshiren.com/post"
}

java version

Java needs to use the multiPart() method provided by the given() method. The first parameter is name, and the second parameter needs to pass a File instance object. During the File instantiation process, the absolute path + file name of the uploaded file needs to be passed in. .

import java.io.File;

import static io.restassured.RestAssured.*;

public class Requests {
public static void main(String[] args) {
given().multiPart("hogwartsFile", new File("绝对路径+文件名")).
when().post("https://httpbin.ceshiren.com/post").then().log().all();
}
}


The response content is

{
"args": {
},
"data": "",
"files": {
"hogwarts_file": "123"
},
"form": { 
},
"headers": {
...省略...
},
"json": null,
"origin": "119.123.207.174",
"url": "https://httpbin.ceshiren.com/post"
}

insert image description here

Use the packet capture tool to grab the process data, and you can clearly see that in the process of transferring data, if it is the Java version, the content of the name transfer is the first parameter of the multiPart() method, and in the Python version, the key of the dictionary passed as the files parameter value, and whether filename is the Java version or the Python version, the passed content is the file name of the passed file.


              [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

2. Interface automation project actual combat 

3. Actual Combat of Web Automation Project


4. Actual Combat of App Automation Project 

5. Resume of first-tier manufacturers


6. Test and develop DevOps system 

7. Commonly used automated testing tools

Eight, JMeter performance test 

9. Summary (little surprise at the end)

life is long so add oil. Every effort will not be let down, as long as you persevere, there will be rewards in the end. Cherish your time and pursue your dreams. Don't forget the original intention, forge ahead. Your future is in your hands!

Life is short, time is precious, we cannot predict what will happen in the future, but we can grasp the present moment. Cherish every day and work hard to make yourself stronger and better. Firm belief, persistent pursuit, success will eventually belong to you!

Only by constantly challenging yourself can you constantly surpass yourself. Persist in pursuing your dreams and move forward bravely, and you will find that the process of struggle is so beautiful and worthwhile. Believe in yourself, you can do it! 

Guess you like

Origin blog.csdn.net/nhb687095/article/details/131980286