pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)
前言
使用 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+yaml 框架使用与开发》https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338