Why is the simplest request_mock example not working with pytest?

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

I tried using the first example in request_mock docs , except that I put it in the test_mock () function and added an assert -statement for pytest to detect it.

The following code does not work:

 # 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' 

Since I can make requests_mock work with ipython, I assume the problem is with pytest, but I could be wrong.

It seems that the adapter is not used at all, so the requests send a real HTTP request to the destination URL instead of using a mocking object.




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

Derive from running code:

 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 =========================== 
+1
python mocking python-requests
Dec 07 '17 at 21:04 on
source share
1 answer

Why it works in IPython seems a mystery to me. To fix the problem, simply replace the function definition lines with 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' 
+1
Dec 08 '17 at 15:42 on
source share



All Articles