前言

使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.
当出现这个警告的时候,我们第一反应是加忽略警告:urllib3.disable_warnings(),然而并不管用。

问题描述

使用requests库发https请求,添加verify=False忽略证书

# test_https.py
import requests
import urllib3
urllib3.disable_warnings()def test_h():'''author: 上海-悠悠 QQ交流群:779429633blog: https://www.cnblogs.com/yoyoketang:return: '''url = "https://www.cnblogs.com/yoyoketang"s = requests.session()s.verify = Falser = s.get(url)assert "上海-悠悠" in r.text

命令行使用pytest运行用例

D:\demo>pytest test_https.py
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
rootdir: D:\demo
plugins: allure-pytest-2.8.6
collected 1 itemtest_https.py .                                                          [100%]============================== warnings summary ===============================
test_https.py::test_he:\python36\lib\site-packages\urllib3\connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made.
Adding certificate verification is strongly advised.
See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)-- Docs: https://docs.pytest.org/en/latest/warnings.html
==================== 1 passed, 1 warnings in 0.35 seconds =====================

这时候会出现 InsecureRequestWarning 警告,去百度搜都是上加上这句

urllib3.disable_warnings()

然而你会发现不管用

问题分析

出现这个问题,并不是因为 ‘urllib3.disable_warnings()’ 不生效,主要是小伙伴门对 pytest 的运行规则不熟悉,pytest 框架运行的时候会查找test_.py文件下的test_()函数或方法的用例
也就是只会执行 test_h() 下面的代码,所以根本就不会执行它上面的代码,可以试试换个位置,放到test_h() 以下,就会生效

import requests
import urllib3
# urllib3.disable_warnings()def test_h():'''author: 上海-悠悠 QQ交流群:779429633blog: https://www.cnblogs.com/yoyoketang:return:'''urllib3.disable_warnings()  # 换个位置url = "https://www.cnblogs.com/yoyoketang"s = requests.session()s.verify = Falser = s.get(url)assert "上海-悠悠" in r.text

再次运行 pytest test_https.py 警告就没有了

warnings 文档

上面的警告内容有个doc文档地址Docs: https://docs.pytest.org/en/latest/warnings.html,点开查询解决方案

文档上有对于警告出现的详细描述,在命令行添加--disable-warnings 参数忽略警告

pytest test_https.py --disable-warnings

D:\demo>pytest test_https.py --disable-warnings
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
rootdir: D:\demo
plugins: allure-pytest-2.8.6
collected 1 itemtest_https.py .                                                          [100%]==================== 1 passed, 1 warnings in 0.24 seconds =====================

虽然警告内容没有了,但是警告还是会显示:1 passed, 1 warnings

也许你想彻底的不想看到warnings,可以不加载 warnings 插件,使用-p参数忽略插件加载

 -p name               early-load given plugin module name or entry point(multi-allowed). To avoid loading of plugins, use the`no:` prefix, e.g. `no:doctest`.

带上 -p 参数运行

pytest test_https.py -p no:warnings

D:\demo>pytest test_https.py -p no:warnings
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
rootdir: D:\demo
plugins: allure-pytest-2.8.6
collected 1 itemtest_https.py .                                                          [100%]========================== 1 passed in 0.29 seconds ===========================

现在可以看到运行结果里面完全没有 warnings 字样了

可以在项目根目录放一个pytest.ini文件,内容如下

[pytest]
addopts = -p no:warnings

这样使用命令行执行的时候,就可以不用每次都带-p参数了

pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)相关推荐

  1. requests库提示警告:InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate ver

    相信你们肯定遇到过这样的问题: requests库提示警告:InsecureRequestWarning: Unverified HTTPS request is being made. Adding ...

  2. 【解决】Requests库的SSL警告:InsecureRequestWarning: Unverified HTTPS request is being made.

    Requests库的SSL警告:InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate v ...

  3. requests 警告:InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verif

    最近爬12306,没有安装证书.每次请求都会有下面的ss警告,看到不是很舒服. InsecureRequestWarning: Unverified HTTPS request is being ma ...

  4. 进行requests请求时:InsecureRequestWarning: Unverified HTTPS request is being made to host 127.0.0.1. Ad

    用requests进行页面请求时,出现不安全请求的警告: 解决方法: 在请求前加上一段代码即可 import urllib3 #导入urllib3

  5. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  6. pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)

    前言 pytest-html报告中当用到参数化时候,获取用例的nodeid里面有中文时候,会显示[\u6350\u52a9\u6211\u4eec]这种编码(再次声明,这个不叫乱码,这是unicode ...

  7. 打开Word文档的时候提示 “安全警告 宏已被禁用”

    1.遇到问题 在打开word文档的时候提示"安全警告 宏已被禁用" 2.问题解决 选择[选项] 出现如下界面: 3.问题成功解决

  8. InsecureRequestWarning: Unverified HTTPS request is being made to host (https请求警告)

    报错信息如下: E:\install_path\Python\Python38-32\lib\site-packages\urllib3\connectionpool.py:981: Insecure ...

  9. InsecureRequestWarning: Unverified HTTPS request is being made to host(requests证书警告)

    本文仅供学习交流使用,如侵立删!demo下载见文末 InsecureRequestWarning: Unverified HTTPS request is being made to host(req ...

最新文章

  1. 数据结构与算法(6-4)线索二叉树
  2. 微软终于想通把Script56文档更新了
  3. MySQL插入数据时报错Cause: java.sql.SQLException: #HY000的解决方法
  4. Ubuntu18.04安装rabbitmq
  5. think.class.php下载,PHP_ThinkPHP实现将本地文件打包成zip下载,首先,将FileToZip.class文件放到T - phpStudy...
  6. 关于考研与工作(人生规划)的个人思考
  7. python 读取地震道头数据_python地震数据可视化详解
  8. 随机数插入排序c 语言,C语言程序设计100例之(22):插入排序
  9. STM32F103DAC功能实现
  10. 有人说赚钱靠项目,也有人说赚钱靠技术
  11. 有道云笔记快捷键使用
  12. [python小工具]小说分割器
  13. 时序分析 27 - 时序预测 格兰杰因果关系(上) 理论知识
  14. How To Verify the Word Size (32bit vs 64bit) of the Oracle Binary on MS Windows systems (文档 ID 46506
  15. c++ 输入一个日期,计算出该日期是这一年中的第几天(switch语句)
  16. 如何使用js实现交通红绿灯
  17. 一份来自于全球的前端面试题清单,看看老外喜欢考哪些题(部分有答案)
  18. python读取手机短信信息_python 自动获取手机短信验证码
  19. FVCOM 环境基础配置(1) intel编译器 下载与安装
  20. 广州大学城热水解锁卡dump

热门文章

  1. Python画笑脸(朋友的头像)
  2. FFMPEG总结 -- 音视频编解码和转码(全)
  3. 分享回顾|新岁序开,2023 和Jina AI共同码梦!
  4. Python分析淘宝月饼销售数据,五仁月饼王者地位不可动摇!
  5. python多线程案例
  6. XYZprinting:3D打印让你随心所欲打造专属美味
  7. 微信直播服务器架构,音视频直播--技术架构—易龙天
  8. 上云网关EasyNTS遇到IP冲突时,如何正确更改IP地址?
  9. List集合,Set集合
  10. Python matplotlib 实现DDA算法