Riccardo79
Riccardo79

Reputation: 1056

InsecureRequestWarning + 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?

Upvotes: 13

Views: 4561

Answers (2)

Glen Carpenter
Glen Carpenter

Reputation: 412

Just wanted to put this little code snippet out there for Django users- Add this to your imports:

import urllib3

Then add this to disable InsecureRequestWarning for all the tests on your file:

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

Upvotes: 0

hoefling
hoefling

Reputation: 66461

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.*

Upvotes: 21

Related Questions