[Detailed practical explanation] How to quickly build an interface automation testing framework? Python + Requests

Summary:

This article mainly introduces how to use the Python language and the Requests library for automated interface testing, and provides detailed code examples and operation steps. I hope it can be inspiring and helpful to readers.

Preface

With the rapid development of mobile Internet, more and more applications adopt Web API (also known as RESTful API) as the main method of data exchange. Automated testing for APIs has become very important. It allows us to quickly detect and verify the correctness and reliability of APIs, laying the foundation for subsequent product development, testing, and deployment.

This article will introduce how to use Python language and Requests library for automated interface testing. Through actual cases, it demonstrates how to design test cases, prepare test data, write automation scripts, etc., in order to better achieve the goals of interface automation testing.

Environmental preparation

Before starting automated interface testing, you need to prepare the following environment:

Python 3.x

Requests library

Test server address and account password

Among them, the Requests library is the mainstream HTTP request library in Python, which can easily perform GET, POST and other requests and process the response results. Readers can install the Requests library through the pip command:

pip install requests

Design test cases

Before conducting automated interface testing, detailed test cases need to be designed first. Test cases should cover various functions and exceptions of the API, and take into account the reusability and maintainability of the system. Here is a simple test case:

API name: Get user information

API URL:http://localhost:8080/user_info

Request method: GET

Request parameters: user_id (string)

Response result: JSON format, including user ID, user name, email and other information

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

 

Prepare test data

Design test data based on test cases and ensure that the test data conforms to actual usage scenarios and conditions. Attention needs to be paid to the randomness and uniqueness of data to avoid duplication or misjudgment. Here are two test data examples:

Test data 1:

user_id = "123"

Test data 2:

user_id = "456"

Write automation scripts

Use Python language and Requests library to write automation scripts, execute test cases and check test results. During the testing process, you need to pay attention to recording and troubleshooting errors, and provide timely feedback to the developers.

import requests

# 测试数据
data1 = {
"user_id": "123"
}

data2 = {
"user_id": "456"
}

# 请求头
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer xxxxxxxxxxxxxxx"
}

# API地址
url = "http://localhost:8080/user_info"

# 发送请求
res1 = requests.get(url, params=data1, headers=headers)
res2 = requests.get(url, params=data2, headers=headers)

# 检查响应结果
assert res1.status_code == 200
assert res2.status_code == 200

# 解析JSON结果
result1 = res1.json()
result2 = res2.json()

# 检查数据完整性
assert result1["user_id"] == "123"
assert result2["user_id"] == "456"

Code analysis:

Lines 1 and 2: Import the requests library.

Lines 5 and 9: Set up test data, including two different user_ids.

Line 13: Set request headers, including Content-Type and Authorization.

Line 16: Set the API address.

Lines 19 and 20: Use the requests library to send a GET request and pass test data and request headers.

Lines 23 and 24: Check the response result. If the returned status code is 200, it means the API request is successful.

Lines 27 and 28: Parse the results in JSON format and store the results in result1 and result2.

Lines 31 and 32: Check data integrity. If the user_id matches the expected value, the test case passes.

Run automation script

Use command line tools or an integrated development environment (IDE) to run automation scripts. Make sure the test server starts normally and provide the correct account password.

python test_api.py

If all goes well, you should see the following output:

{'user_id': '123', 'username': 'jimmy', 'email': '[email protected]'}
{'user_id': '456', 'username': 'lucy', 'email': '[email protected]'}

This shows that the automated test script successfully requested the interface and obtained the correct response result.

in conclusion

In this article, we introduce how to use the Python language and the Requests library for automated interface testing. By designing test cases, preparing test data, and writing automated scripts, you can quickly detect and verify the correctness and reliability of the API, laying the foundation for subsequent product development, testing, and deployment. Readers can further optimize and expand automated testing solutions to improve testing efficiency and quality based on actual needs and conditions.

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/132840517
Recommended