Advertisement

使用Python和requests进行接口测试

阅读量:

此前已有诸多文献详细阐述了利用Jmeter实施接口测试的方法,但实际上通过Python同样能够实现接口测试,且操作更为简便。

Requests库安装指南

首先需要通过pip install requests命令来安装requests依赖库

假设有如下接口:当用户提交姓名和地点信息后,系统将返回对应的餐厅信息:

接口地址:http://pythontest.com/getrestaurant/

请求参数:username、location

Get方法

当采用Get方法时,可运用以下代码进行测试:

复制代码
 #coding=utf-8

    
 import requests
    
  
    
 url = "http://pythontest.com/getrestaurant?"
    
 # 接口请求的URL
    
 headers = {
    
         "Connection": "keep-alive",
    
         "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, lik

全部评论 (0)

还没有任何评论哟~