一、pytest接口自动化教程--requests请求

本文介绍了如何在PyCharm中使用pytest进行单元测试,并列举了PyCharm中与requests库相关的常见快捷键,如GET/POST请求、添加headers和使用session。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pytest是python的一种单元测试框架

  • pytest框架总览

在这里插入图片描述

  • pycharm常用快捷键

1、 Ctrl+B / Command+B 查看详细参数
2、 Ctrl+F7 查看函数的使用情况
3、 按住Ctrl,鼠标点击查看的函数,,会直接跳转到函数的源码
4、 Ctrl + Alt + L 格式化代码
5、 Alt + Enter:优化代码,提示信息实现自动导包
6、 Ctrl + N 查找所有的类的名称
7、 快速导入模块:Alt+enter
8、Ctrl+D:快速复制一行代码
9、Ctrl+鼠标左键:查看模块详情

  • 官方文档地址

https://requests.readthedocs.io/en/latest/
● 安装包
1、安装requests

pip/pip3 install requests

2、安装pytest

pip/pip3 install pytest

requests请求

(一)request进行get请求

无参数

r = requests.get('https://api.github.com/events')

有参数

r = requests.get('https://httpbin.org/post', data={'key': 'value','key': 'value'})

(二)requests模块进行post请求

(1)传递params格式的参数

'''
@Author     : 测试工程师Selina
@FileName   : test_requests_params.py
@Description: 
'''
import requests
params = {'key': 'value','key': 'value'}
r = requests.post('http://ip地址:端口号/outPatient/reception/getDoctorDept', params=params)
print(r.json())

(2)传递json格式的参数

'''
@Author     : 测试工程师Selina
@FileName   : test_requests_json.py
@Description: 
'''
import requests 
json_data = {"Keys": {
    "workerId": 10397,
    "hospitalCode": 1
}}
url = 'http://ip地址:端口号/outPatient/reception/getDoctorDept'
r = requests.post(url=url, json=json_data)
print(r.status_code)
print(r.json())

(三)requests请求加入hearders

'''
@Author     : 测试工程师Selina
@FileName   : test_requests_headers.py
@Description: 
'''
import requests

headers = {"User-Agent": "PostmanRuntime/7.29.2"}
json_data = {"Keys": {
    "workerId": 10397,
    "hospitalCode": 1
}}
url = 'http://ip地址:端口号/outPatient/reception/getDoctorDept'
r = requests.post(url=url, json=json_data, headers=headers)
print(r.status_code)
print(r.json())

(四)requests请求session用法

'''
@Author     : 测试工程师Selina
@FileName   : test_session.py
@Description: 
'''
import requests
"""这种方式不需要一直录cookie或者session"""

##创建一个会话机制

req = requests.Session()
headers = {"User-Agent": "PostmanRuntime/7.29.2"}
json_data = {"Keys": {
    "workerId": 10397,
    "hospitalCode": 1
}}
url = 'http://ip地址:端口号/outPatient/reception/getDoctorDept'

##登录,req保存了cookie或者session,注意requests.post需要替换为req.post


r = req.post(url=url, json=json_data, headers=headers)
print(r.status_code)
'''
@Author     : 测试工程师Selina
@FileName   : test_session.py
@Description: 
'''
import requests

"""这种方式需要在cookie或者session过期后重新进行替换"""
headers = {"cookie": "token=复制网页的token信息"}
json_data = {"Keys": {
    "workerId": 10397,
    "hospitalCode": 1
}}
url = 'http://ip地址:端口号/outPatient/reception/getDoctorDept'
r = requests.post(url=url, json=json_data, headers=headers)
print(r.status_code)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Selina 0_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值