Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the simplest requests_mock example fail with pytest?

I have a peculiar problem with requests_mock. I want to use it with pytest to test my API wrapper library.

I've tried to use the first example in the requests_mock docs, except I put it in a test_mock()-function and added an assert-statement for pytest to discover it.

The following code fails:

# tests/test_mock.py

import requests
import requests_mock

with requests_mock.Mocker() as m:
    def test_mock():
        m.get('http://test.com', text='resp')
        assert requests.get('http://test.com').text == 'resp'

However, when running the following example in ipython, it works as expected. This is the exact code from the example:

with requests_mock.Mocker() as m:
    m.get('http://test.com', text='resp')
    print(requests.get('http://test.com').text)

# prints 'resp'

Because I can make requests_mock work from ipython, I assume the issue is with pytest, but I might be wrong.

It seems like the adapter isn't used at all, so that requests sends a real http request to the target url instead of using the mocked object.


I'm using Python 3.6.3 (Anaconda3), requests_mock 1.3.0 and pytest 3.3.0

Output from running the code that fails:

C:\dev\mylib>pytest -v
============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-3.3.0, py-1.5.2, pluggy-0.6.0 -- e:\anaconda3\envs\benv\python.exe
cachedir: .cache
rootdir: C:\dev\mylib, inifile: setup.cfg
collected 1 item

tests/test_mocks.py::test_mock FAILED                                    [100%]

================================== FAILURES ===================================
__________________________________ test_mock __________________________________

    def test_mock():
        m.get('http://test.com', text='resp')
>       assert requests.get('http://test.com').text == 'resp'
E       assert '<!DOCTYPE ht...y>\n</html>\n' == 'resp'
E         + resp
E         - <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
E         - <html>
E         - <head>
E         - <title>Client Validation</title>
E         - <script type="text/javascript">
E         - function setCookie(c_name, value, expiredays) {...
E
E         ...Full output truncated (26 lines hidden), use '-vv' to show

tests\test_mocks.py:9: AssertionError
------------------------------ Captured log call ------------------------------
connectionpool.py          208 DEBUG    Starting new HTTP connection (1): test.com
connectionpool.py          396 DEBUG    http://test.com:80 "GET / HTTP/1.1" 302 161
connectionpool.py          824 DEBUG    Starting new HTTPS connection (1): www.test.com
connectionpool.py          396 DEBUG    https://www.test.com:443 "GET / HTTP/1.1" 200 None
========================== 1 failed in 2.05 seconds ===========================
like image 424
Thomas Fauskanger Avatar asked Dec 07 '17 21:12

Thomas Fauskanger


1 Answers

Why it works in IPython seems like a mystery to me. To fix the issue just swap the lines of the function definition with the opening of the context manager.

# tests/test_mock.py

import requests
import requests_mock

def test_mock():
    with requests_mock.Mocker() as m:
        m.get('http://test.com', text='resp')
        assert requests.get('http://test.com').text == 'resp'
like image 75
Ignacio Vergara Kausel Avatar answered Oct 17 '22 07:10

Ignacio Vergara Kausel