私はrequests_mock
に特有の問題があります。 APIラッパーライブラリーをテストするのにpytest
と一緒に使用したいと思います。私はそれを発見するpytestためassert
-statementをtest_mock() -functionに入れて、コメントを追加除い最も簡単なrequests_mockの例がpytestで失敗するのはなぜですか?
私は、first example in the requests_mock docs使用することを試みました。
次のコードは失敗します。
# 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'
ipythonで、次の例を実行すると、期待どおりしかし、それは動作します。私はipythonからrequests_mock
作業を行うことができますので、私は問題がpytestであると仮定し、私は間違っているかもしれない
with requests_mock.Mocker() as m:
m.get('http://test.com', text='resp')
print(requests.get('http://test.com').text)
# prints 'resp'
:これはexampleから正確なコードです。
アダプタがまったく使用されていないように見えるので、要求は模擬オブジェクトを使用する代わりに実際のHTTP要求をターゲットURLに送信します。
私は、Python 3.6.3(Anaconda3)、requests_mock 1.3.0および失敗したコードを実行しているからpytest 3.3.0
出力使用しています:それは作品はなぜ
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 ===========================
私はipythonで動作し、最初のものではなく、私の質問で_second_コードセクションであることを強調することができます。私はいくつかの方法で同じMockerを再利用したいと思っていましたが、ここで間違いがどこにあるのか分かります。ありがとう –
私はあなたがクラスにテストを入れ、それを飾ることができると思います(私は今それを確認することはできません)。とにかくありがとう。 –
私はクラスを装飾しようとしましたが、私のシステム上では他の引数と競合しているようですので、kw引数を[docs](https:// requests-mock .readthedocs.io/en/latest/mocker.html#decorator)。これがpytestと関係があるのかどうかは分かりませんが、上記のエラーはfixturesを参照してください。 –