前言

使用 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交流群:779429633
blog: https://www.cnblogs.com/yoyoketang
:return:
'''
url = "https://www.cnblogs.com/yoyoketang"
s = requests.session()
s.verify = False
r = 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 item test_https.py . [100%] ============================== warnings summary ===============================
test_https.py::test_h
e:\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交流群:779429633
blog: https://www.cnblogs.com/yoyoketang
:return:
'''
urllib3.disable_warnings() # 换个位置
url = "https://www.cnblogs.com/yoyoketang"
s = requests.session()
s.verify = False
r = 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 item test_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 item test_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. pytest文档3-pycharm运行pytest

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

  2. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  3. InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.解决办法

    最近使用requests进行get请求的时候,控制台输出如下错误. InsecureRequestWarning: Unverified HTTPS request is being made. Ad ...

  4. 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 In

    InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is s ...

  5. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  6. pytest文档1-环境准备与入门

    前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...

  7. pytest文档56-插件打包上传到 pypi 库

    前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...

  8. pytest文档55-plugins插件开发

    前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...

  9. pytest文档51-内置fixture之cache使用

    前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...

随机推荐

  1. vue父子组件状态同步的最佳方式

    哈喽!大家好!我是木瓜太香,一位老牌儿前端工程师,平时我们在使用 vue 开发的时候,可能会遇到需要父组件与子组件某个状态需要同步的情况,通常这个是因为我们封装组件的时候有一个相同的状态外面要用,里面 ...

  2. VUE常用问题hack修改

    vue-router router这里踩的坑主要是组件的重用.构建单页面大型应用的话,肯定要开启组件的缓存的,因为一般会要求后退的时候不要重新加载页面,而且要记住原始的滚动位置.首先,引入router ...

  3. xml的复习

    xml的复习 1.概念:可扩展标记语言 2.功能: *存储数据    1.配置文件     2.在网络中传播 3.xml与html区别: xml语法严格,HTML语法松散 xml存储数据,HTML展示 ...

  4. mariadb 3

    MariaDB第三章(select)   基本查询 --查询基本使用(条件,排序,聚合函数,分组,分页) --创建学生表 create table students ( id int unsigned ...

  5. hystrix讲解:熔断降级隔离以及合并请求

    对springcloud只是学习了基本的框架搭建,基本上看到的例子都是只使用了fallback 但是hystrix还有线程隔离和请求合并的能力  顺便吐槽   大部分人的博客例子估计都是听课的  应用 ...

  6. Docker之概述

    我们常常需要将应用程序部署在独立的系统环境中,而使用物理机器部署会浪费大量的物理资源.能否讲物理机器虚拟成一个一个容器,而将程序部署在各自的容器中?Docker是一个能够把开发的应用程序自动部署到容器 ...

  7. python的多种魔术方法

    目录 new str & repr iter getitem.setitem.delitem getattr.setattr.delattr call slots 定制类和魔法方法 new s ...

  8. Orchard Core创建CMS/Blog站点

    安装.NET Core SDK 下载并安装当前最新版本.NET Core SDK 3.1: https://dotnet.microsoft.com/download 安装visual studio ...

  9. 【Netty之旅四】你一定看得懂的Netty客户端启动源码分析!

    前言 前面小飞已经讲解了NIO和Netty服务端启动,这一讲是Client的启动过程. 源码系列的文章依旧还是遵循大白话+画图的风格来讲解,本文Netty源码及以后的文章版本都基于:4.1.22.Fi ...

  10. 研究生杂谈-1粗粒度(Coarse-grained)VS细粒度(fine-grained)

    粒度似乎是根据项目模块划分的细致程度区分的,一个项目模块(或子模块)分得越多,每个模块(或子模块)越小,负责的工作越细,就说粒度越细,否则为粗粒度. 简而言之: 粗粒度:模块的功能太过于集中. 细粒度 ...