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:
Thomas Fauskanger Dec 07 '17 at 21:04 on 2017-12-07 21:04
source share