InsecureRequest警告+ pytest

时间:2019-07-04 05:34:44

标签: python ssl pytest

我在pytest摘要上仍然有SSL警告。
Python 2.7.5
requests==2.22.0
urllib3==1.25.3
pytest version 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警告?

2 个答案:

答案 0 :(得分:1)

重新整理评论:只有重新打开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:中的单冒号或双冒号之间的区别)。

完整的测试套件

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

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

或通过警告消息:

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

答案 1 :(得分:0)

只想为Django用户提供这个小代码段-将其添加到您的导入中:

import urllib3

然后添加此选项以对文件上的所有测试禁用InsecureRequestWarning:

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)