Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Excel+Python runs batch API in ten minutes, which is so simple, rough and easy to use.

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

API testing is now being valued by more and more companies, and the first thing for test engineers to do after each request is to study the requirements document. Since interfaces are a way for two independent systems to synchronize data or access each other's programs, we should first look at which two systems these interfaces connect and what their relationship is. Therefore, the general requirements document will include interface name, interface description, interface type, interface address, push parameters, and return parameters. Those of you familiar with Postman will find that these are all required parts:

With these items, we need to convert the requirements document to Excel, so that it is relatively easy to do case management and compare results. Later Python is also easier to read and write. In Excel, we write only the parts that change, while for relatively fixed ones, such as server information, we parameterize them directly in Python code. So the input document that comes out is as follows:

With that in mind, we'll use xlrd and xlwt packages to process excel, so we won't explain how to use xlrd and xlwt too much in this article. Why create Workbook2? Because after running once, if we find that some interfaces run out of the results have problems, we can not close workbook1, directly modify, and then rerun, will generate a new workbook2, overwrite the previous results, save time to open and close the workbook.

workbook1 = xlrd.open_workbook(r"C:\Demo\API\API_Cases.xlsx")

workbook2 = xlwt.Workbook(encoding='utf-8')

sheet1 = workbook1.sheet_by_index(0)

sheet2 = workbook2.add_sheet("response")

After reading the content, we modify the part that needs to be parameterized. In general, the first half of the url is http://xx.xx.xx:8080 unchanged, so we only need to take out the second column in Excel and splice it into url. The same is true for the body part. If there is a fixed part of the body, we can define it directly in python, and then splice it with the content in Excel:

par_url = sheet1.col_values(1)

body = sheet1.col_values(2)

Of course, the splicing code needs to be written inside the loop:

request_url = 'https://postman-echo.com' + par_url[i]

print(request_url)

When executing, we use the requests package. If there are GET, POST, PUT and Delete in the request, we can handle them separately because of the different parameters used. For different headers that need to be used for different requests, we can also define them in the loop. If there is a big difference, we can also define a separate column in excel, and then add the corresponding reading statement to the code. For POST type requests, we need the following parameters:

response = requests.post(url=request_url, headers={}, data=body[i])

print(body[i])

After execution, if you write it directly to excel, we will find that after opening the response file, all the returned content has become a line, which is very inconvenient to analyze. So we need to process the returned data first and then write it to excel. If the returned content contains Chinese, ensure_ascii=False must be added, otherwise coding problems will occur.

json_dic = json.dumps(json.loads(response.content), indent=4, ensure_ascii=False)

Finally, we write the sorted output into a new excel document and save it, and we're done.

sheet2.write(i, 0, json_dic)

workbook2.save(r"C:\Demo\API\response.xls")

Some of them encountered problems, but also here tips, for example, in the POST request, some parameters are random numbers, resulting in the need to manually modify each time the request is sent, otherwise it will report an error such as "already exists", at this time we need to use the random package. Let's take generating a 10-bit random number as an example. Replace {{random}} in the body with the generated 10-bit random number in the code:

random_num = str(math.floor(1e10 * random.random()))

response = requests.post(url=request_url, headers={}, data=body[i].replace('{{random}}', random_num))

The operation results are shown in the figure:

New files generated:

Open response.xls and you can see that 50 APIs have been run. Click on A1 to see the specific content of the response:

Add me VX: 17324089390 Reply keyword "test" Get limited software test learning materials Oh ~~

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report