gpt4 book ai didi

python - 不安全请求警告 + pytest

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:56 28 4
gpt4 key购买 nike

我在 pytest 摘要上仍然有 SSL 警告。
Python 2.7.5
请求==2.22.0
urllib3==1.25.3
pytest 版本 4.3.1

这是代码:

import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def test_x():
response = requests.get("https:// ...",
verify=False)
print response.text

输出pytest mytest.py:

....    
==================================================== warnings summary ====================================================
prova.py::test_x
/usr/lib/python2.7/site-packages/urllib3/connectionpool.py:851: 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.30 seconds ==========================================

如何从 pytest 中删除 SSL 警告?

最佳答案

重申评论:您只能通过重新打开 SSL 证书验证来删除它。但是,您可以隐藏它(因此仍然会发出警告,但不会显示在警告部分):

选定的测试

通过警告类将 pytest.mark.filterwarnings 标记应用于测试:

@pytest.mark.filterwarnings('ignore::urllib3.exceptions.InsecureRequestWarning')
def test_example_com():
requests.get('https://www.example.com', verify=False)

或通过警告消息:

@pytest.mark.filterwarnings('ignore:Unverified HTTPS request is being made.*')
def test_example_com():
requests.get('https://www.example.com', verify=False)

(区别在于 ignore::ignore: 中的单冒号或双冒号)。

完整的测试套件

pytest.ini 中配置 filterwarnings,也可以通过警告类:

[pytest]
filterwarnings =
ignore::urllib3.exceptions.InsecureRequestWarning

或通过警告消息:

[pytest]
filterwarnings =
ignore:Unverified HTTPS request is being made.*

关于python - 不安全请求警告 + pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56881233/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com