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)