Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InsecureRequestWarning + pytest

Tags:

python

ssl

pytest

I still have the SSL warning on pytest summary.
Python 2.7.5
requests==2.22.0
urllib3==1.25.3
pytest version 4.3.1

This is the code:

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

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

The output 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 ==========================================

How can i remove the SSL warning from pytest?

like image 981
Riccardo79 Avatar asked Jul 04 '19 05:07

Riccardo79


People also ask

How do I ignore warnings in Pytest?

Use warnings. filterwarnings() to ignore deprecation warnings Call warnings. filterwarnings(action, category=DeprecationWarning) with action as "ignore" and category set to DeprecationWarning to ignore any deprecation warnings that may rise.

What is insecure request warning?

From https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings: InsecureRequestWarning. This happens when a request is made to an HTTPS URL without certificate verification enabled. Follow the certificate verification guide to resolve this warning.


1 Answers

Restating the comment: You can remove it only by turning the SSL cert verification back on. You can, however, hide it (so the warning is still emitted, but not displayed in the warnings section):

selected tests

applying the pytest.mark.filterwarnings marker to a test, either by warning class:

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

or by warning message:

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

(the difference is between single or double colons in ignore:: and ignore:).

complete test suite

Configure filterwarnings in the pytest.ini, also either by warning class:

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

or by warning message:

[pytest]
filterwarnings =
    ignore:Unverified HTTPS request is being made.*
like image 154
hoefling Avatar answered Oct 29 '22 14:10

hoefling