为什么最简单的request_mock示例在pytest中失败? [英] Why does the simplest requests_mock example fail with pytest?

查看:243
本文介绍了为什么最简单的request_mock示例在pytest中失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对requests_mock有一个特殊的问题.我想将其与pytest一起使用以测试我的API包装器库.

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

我已经尝试在requests_mock文档中使用第一个示例,除了我将其放在 test_mock()函数中,并为pytest添加了assert语句以发现它.

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.

以下代码失败:

# 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中运行以下示例时,它可以按预期运行.这是来自示例:

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'

因为我可以通过ipython使requests_mock工作,所以我认为问题出在pytest,但是我可能错了.

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

似乎根本没有使用适配器,因此请求将实际的http请求发送到目标url,而不是使用模拟对象.

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.

我正在使用Python 3.6.3(Anaconda3),requests_mock 1.3.0和pytest 3.3.0

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 ===========================

推荐答案

为什么它在IPython中起作用对我来说似乎是一个谜.要解决该问题,只需将函数定义的行与上下文管理器的打开互换即可.

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'

这篇关于为什么最简单的request_mock示例在pytest中失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆