title: Why does the simplest requests_mock example fail with pytest?
body: <p>I have a peculiar problem with <code>requests_mock</code>. I want to use it with <code>pytest</code> to test my API wrapper library.</p>
  
  <p>I've tried to use the <a href="http://requests-mock.readthedocs.io/en/latest/mocker.html" rel="nofollow noreferrer">first example in the requests_mock docs</a>, except I put it in a <em>test_mock()</em>-function and added an <code>assert</code>-statement for pytest to discover it.</p>
  
  <p>The following code fails:</p>
  
  <pre><code># 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'
  </code></pre>
  
  <p>However, when running the following example in ipython, it works as expected. This is the exact code from the <a href="http://requests-mock.readthedocs.io/en/latest/mocker.html" rel="nofollow noreferrer">example</a>:</p>
  
  <pre><code>with requests_mock.Mocker() as m:
      m.get('http://test.com', text='resp')
      print(requests.get('http://test.com').text)
  
  # prints 'resp'
  </code></pre>
  
  <p>Because I can make <code>requests_mock</code> work from ipython, I assume the issue is with pytest, but I might be wrong.</p>
  
  <p>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.</p>
  
  <hr>
  
  <p>I'm using Python 3.6.3 (Anaconda3), requests_mock 1.3.0 and pytest 3.3.0</p>
  
  <p>Output from running the code that fails:</p>
  
  <pre><code>C:\dev\mylib&gt;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')
  &gt;       assert requests.get('http://test.com').text == 'resp'
  E       assert '&lt;!DOCTYPE ht...y&gt;\n&lt;/html&gt;\n' == 'resp'
  E         + resp
  E         - &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  E         - &lt;html&gt;
  E         - &lt;head&gt;
  E         - &lt;title&gt;Client Validation&lt;/title&gt;
  E         - &lt;script type="text/javascript"&gt;
  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 ===========================
  </code></pre>