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參數了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM