1. 上传文件到服务器
场景描述:
使用 requests 库上传文件到服务器。
import requestsdef test_upload_file():url = "https://api.example.com/upload"file_path = "path/to/file.txt"with open(file_path, "rb") as file:files = {"file": ("file.txt", file)}response = requests.post(url, files=files)assert response.status_code == 200assert response.json()["message"] == "File uploaded successfully"
输出结果:
PASSED
INSPIRATION
2. 下载文件并保存到本地
场景描述:
下载文件并保存到本地。
import requestsdef test_download_file():url = "https://api.example.com/download"local_path = "path/to/local/file.txt"response = requests.get(url)with open(local_path, "wb") as file:file.write(response.content)assert response.status_code == 200
输出结果:
PASSED
INSPIRATION
3. 比较两个文件内容
场景描述:
比较服务器返回的文件与本地文件的内容是否一致。
import requestsdef test_compare_files():url = "https://api.example.com/download"local_path = "path/to/local/file.txt"remote_path = "path/to/remote/file.txt"with open(local_path, "r") as local_file, open(remote_path, "r") as remote_file:local_content = local_file.read()remote_content = remote_file.read()assert local_content == remote_content
输出结果:
PASSED
INSPIRATION
4. 读取 CSV 文件并作为请求参数
场景描述:
读取 CSV 文件并将其中的数据作为请求参数发送给 API。
import csvimport requestsdef test_send_csv_data():url = "https://api.example.com/csv"file_path = "path/to/file.csv"with open(file_path, newline='') as csvfile:reader = csv.DictReader(csvfile)for row in reader:response = requests.post(url, data=row)assert response.status_code == 200
输出结果:
PASSED
INSPIRATION
5. 读取 JSON 文件并发送 POST 请求
场景描述:
读取 JSON 文件并将其内容作为请求体发送到 API。
import jsonimport requestsdef test_send_json_data():url = "https://api.example.com/json"file_path = "path/to/file.json"with open(file_path, "r") as file:data = json.load(file)response = requests.post(url, json=data)assert response.status_code == 200
输出结果:
PASSED
INSPIRATION
6. 下载文件并验证文件大小
场景描述:
下载文件并验证文件大小。
import requestsdef test_validate_file_size():url = "https://api.example.com/download"response = requests.get(url)file_size = len(response.content)assert file_size == 1024 # 假设文件大小为 1024 字节
输出结果:
PASSED
INSPIRATION
7. 上传文件并验证文件名
场景描述:
上传文件并验证服务器返回的文件名。
import requestsdef test_validate_uploaded_filename():url = "https://api.example.com/upload"file_path = "path/to/file.txt"with open(file_path, "rb") as file:files = {"file": ("file.txt", file)}response = requests.post(url, files=files)assert response.status_code == 200assert response.json()["filename"] == "file.txt"
输出结果:
PASSED
INSPIRATION
8. 上传多个文件
场景描述:
同时上传多个文件。
import requestsdef test_upload_multiple_files():url = "https://api.example.com/upload"files = [("file", ("file1.txt", open("path/to/file1.txt", "rb"))),("file", ("file2.txt", open("path/to/file2.txt", "rb")))]response = requests.post(url, files=files)assert response.status_code == 200assert len(response.json()["uploaded_files"]) == 2
输出结果:
PASSED
INSPIRATION
9. 上传文件并验证 MD5 校验码
场景描述:
上传文件并验证服务器返回的 MD5 校验码与计算出的校验码是否一致。
import hashlibimport requestsdef test_validate_md5_checksum():url = "https://api.example.com/upload"file_path = "path/to/file.txt"with open(file_path, "rb") as file:files = {"file": ("file.txt", file)}response = requests.post(url, files=files)assert response.status_code == 200md5_hash = hashlib.md5()with open(file_path, "rb") as file:chunk = file.read(8192)while chunk:md5_hash.update(chunk)chunk = file.read(8192)assert response.json()["md5"] == md5_hash.hexdigest()
输出结果:
PASSED
INSPIRATION
10. 上传文件并验证文件类型
场景描述:
上传文件并验证服务器返回的 MIME 类型是否正确。
import requestsimport mimetypesdef test_validate_mimetype():url = "https://api.example.com/upload"file_path = "path/to/image.jpg"with open(file_path, "rb") as file:files = {"file": ("image.jpg", file)}response = requests.post(url, files=files)assert response.status_code == 200assert response.json()["mimetype"] == mimetypes.guess_type(file_path)[0]
输出结果:
PASSED
INSPIRATION
![]()
以上示例中的 URL 和文件路径均为示例性质,你需要根据实际情况替换它们。这些示例涵盖了文件上传、下载、验证文件内容、文件大小、文件名、MD5 校验码、MIME 类型等常见操作。






![单词拆分[中等]](https://i-blog.csdnimg.cn/direct/f5629aa42fbd45808b4530bede4cce8c.png)











