接口测试-requests实例

Posted zhangshan33

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口测试-requests实例相关的知识,希望对你有一定的参考价值。

requests实例

1.代码实例

import json
import requests
url = 'http://localhost:8080'
path = '/login'
full_url = url + path
print('POST 请求的完整url', full_url)

data_dict = {'username': 'zhangshan', 'password': '123456'}
data = json.dumps(data_dict)  # json.dumps 将python类型转成字符串
print('POST 请求参数', data)
headers_dict = {'content_type': 'application/json'}
res = requests.post(full_url, data=data, headers=headers_dict)
print('POST 响应状态码', res.status_code)
print('POST 响应头', res.headers)
print('POST 响应结果', res.text)
print('POST 响应时间', res.elapsed.total_seconds(), '秒')
# json_r = res.json()
# print('POST 响应结果', json_r)

2.封装成函数

1.get请求

import requests
import json
def get(full_url, params_dict, headers_dict):
    # try 抛出异常
    try:
        res = requests.get(full_url, params=params_dict, headers=headers_dict, )
        print('GET 响应状态码', res.status_code)
        print('GET 响应头', res.headers)
        print('GET 响应结果', res.text)
        print('GET 响应时间', res.elapsed.total_seconds(), '秒')
        json_r = res.json()
        print('GET 响应结果.转成python数据类型', json_r)
        return json_r
    except Exception as e:
        print('get错误')
url = 'http://localhost:8080'
path = '/book_info'
full_url = url + path
params_dict = {'bookname': '软件测试', 'checkstatus': 'on'}
headers_dict = {}
res = get(full_url, params_dict, headers_dict)
# 通过业务状态码判断是否成功
if res['error_code'] == 0:
    print('成功')
else:
    print('失败')

2.post请求

import requests
import json
def post_forms(full_url, data1, headers_dict):
    try:
        res = requests.post(full_url, data=data1, headers=headers_dict)
        print('POST 响应状态码', res.status_code)
        print('POST 响应头', res.headers)
        print('POST 响应结果', res.text)
        print('POST 响应时间', res.elapsed.total_seconds(), '秒')
        json_r = res.json()
        print('POST 响应结果', json_r)
        return json_r
    except Exception as e:
        print('post_forms 错误')
url = 'http://localhost:8080'
path = '/login'
full_url = url + path
data_dict = {'username': 'zhangshan', 'password': '123456', }
data1 = json.dumps(data_dict)  # json.dumps 将python类型转成字符串
headers_dict = {'content_type': 'application/x-www-form-urlencoded'}
res2 = post_forms(full_url, data1, headers_dict)
if res['checkstatus'] == 'on':
    print('成功')
else:
    print('失败')
def post_json(full_url, data1, headers_dict):
    try:
        res = requests.post(full_url, data=data1, headers=headers_dict)
        print('POST 响应状态码', res.status_code)
        print('POST 响应头', res.headers)
        print('POST 响应结果', res.text)
        print('POST 响应时间', res.elapsed.total_seconds(), '秒')
        json_r = res.json()
        print('POST 响应结果', json_r)
        return json_r
    except Exception as e:
        print('post_json错误')
url = 'http://localhost:8080'
path = '/login'
full_url = url + path
data_dict = {'username': 'zhangshan', 'password': '123456', }
data1 = json.dumps(data_dict)  # json.dumps 将python类型转成字符串
headers_dict = {'content_type': 'application/json'}
res3 = post_json(full_url, data1, headers_dict)
if res['checkstatus'] == 'on':
    print('成功')
else:
    print('失败')

3.封装成类

类中有三个方法

import requests
import json
class Res(object):
    def get(self, full_url, params_dict, headers_dict):
        # try 抛出异常
        try:
            res = requests.get(full_url, params=params_dict, headers=headers_dict, )
            print('GET 响应状态码', res.status_code)
            print('GET 响应头', res.headers)
            print('GET 响应结果', res.text)
            print('GET 响应时间', res.elapsed.total_seconds(), '秒')
            json_r = res.json()
            print('GET 响应结果.转成python数据类型', json_r)
            return json_r
        except Exception as e:
            print('get错误')
    def post_forms(self, full_url, data1, headers_dict):
        try:
            res = requests.post(full_url, data=data1, headers=headers_dict)
            print('POST 响应状态码', res.status_code)
            print('POST 响应头', res.headers)
            print('POST 响应结果', res.text)
            print('POST 响应时间', res.elapsed.total_seconds(), '秒')
            json_r = res.json()
            print('POST 响应结果', json_r)
            return json_r
        except Exception as e:
            print('post_forms 错误')
    def post_json(self, full_url, data1, headers_dict):
        try:
            res = requests.post(full_url, data=data1, headers=headers_dict)
            print('POST 响应状态码', res.status_code)
            print('POST 响应头', res.headers)
            print('POST 响应结果', res.text)
            print('POST 响应时间', res.elapsed.total_seconds(), '秒')
            json_r = res.json()
            print('POST 响应结果', json_r)
            return json_r
        except Exception as e:
            print('post_json错误')
url = 'http://localhost:8080'
path = '/book_info'
full_url = url + path
params_dict = {'bookname': '软件测试', 'checkstatus': 'on'}
headers_dict = {}
res1 = Res()  # 实例化对象
res1.get(full_url, params_dict, headers_dict)  # 对象.函数方法
url = 'http://localhost:8080'
path = '/login'
full_url = url + path
data_dict = {'username': 'zhangshan', 'password': '123456', }
data1 = json.dumps(data_dict)  # json.dumps 将python类型转成字符串
headers_dict = {'content_type': 'application/x-www-form-urlencoded'}
res2 = Res()
res2.post_forms(full_url, data1, headers_dict)
url = 'http://localhost:8080'
path = '/login'
full_url = url + path
data_dict = {'username': 'zhangshan', 'password': '123456', }
data1 = json.dumps(data_dict)  # json.dumps 将python类型转成字符串
headers_dict = {'content_type': 'application/json'}
res3 = Res()
res3.post_json(full_url, data1, headers_dict)
# if res['checkstatus'] == 'on':
#     print('成功')
# else:
#     print('失败')

以上是关于接口测试-requests实例的主要内容,如果未能解决你的问题,请参考以下文章

Python3简易接口自动化测试框架设计与实现 实例2

一致性哈希算法PHP测试片段

java 代码片段

python3+requests+unittest:接口自动化测试

JMeter接口测试-模块控制器

学习笔记:python3,代码片段(2017)