简体   繁体   English

带有模拟/pytest-mock 的 Pytest

[英]Pytest with mock/pytest-mock

For some reason I cannot get mock.patch to work in any scenario when using Pytest.出于某种原因,在使用 Pytest 时,我无法让 mock.patch 在任何情况下工作。 It simply doesn't do the patching.它根本不进行修补。 Am I using it incorrectly or is something messed up with my configuration?是我使用不当还是我的配置有问题?

base.py基础文件

def foo():
    return 'foo'

def get_foo():
    return foo()

test_base.py test_base.py

import pytest
import mock
from pytest_mock import mocker

from base import get_foo

@mock.patch('base.foo') 
def test_get_foo(mock_foo):
    mock_foo.return_value = 'bar'
    assert get_foo() == 'bar'

def test_get_foo2(mocker):
    m = mocker.patch('base.foo', return_value='bar')
    assert get_foo() == 'bar'

def test_get_foo3():
    with mock.patch('base.foo', return_value='bar') as mock_foo:
        assert get_foo() == 'bar'

pytest results pytest 结果

============================================================= test session starts =============================================================
platform linux2 -- Python 2.7.13, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /projects/git/ABC/query, inifile:
plugins: mock-1.6.0
collected 13 items 

test_base.py .....FFF.....

================================================================== FAILURES ===================================================================
________________________________________________________________ test_get_foo _________________________________________________________________

mock_foo = <MagicMock name='foo' id='140418877133648'>

    @mock.patch('base.foo')
    def test_get_foo(mock_foo):
        mock_foo.return_value = 'bar'
>       assert get_foo() == 'bar'
E       AssertionError: assert 'foo' == 'bar'
E         - foo
E         + bar

test_base.py:67: AssertionError
________________________________________________________________ test_get_foo2 ________________________________________________________________

mocker = <pytest_mock.MockFixture object at 0x7fb5d14bc210>

    def test_get_foo2(mocker):
        m = mocker.patch('base.foo', return_value='bar')
>       assert get_foo() == 'bar'
E       AssertionError: assert 'foo' == 'bar'
E         - foo
E         + bar

test_base.py:71: AssertionError
________________________________________________________________ test_get_foo3 ________________________________________________________________

    def test_get_foo3():
        with mock.patch('base.foo', return_value='bar') as mock_foo:
>           assert get_foo() == 'bar'
E           AssertionError: assert 'foo' == 'bar'
E             - foo
E             + bar

test_base.py:75: AssertionError

The problem was due to the relationship between my import specification and PATH variable.问题是由于我的导入规范和 PATH 变量之间的关系。 If I specified the entire path in the patch argument, like: @mock.patch('<PROJECT_ROOT>.<SUBPACKAGE>.base.foo') where PATH had the parent directory of as an entry, then it worked.如果我在patch参数中指定了整个路径,例如: @mock.patch('<PROJECT_ROOT>.<SUBPACKAGE>.base.foo')其中 PATH 具有作为条目的父目录,那么它就起作用了。 I don't know why it wasn't throwing an import error if it wasn't finding base.foo .如果没有找到base.foo我不知道为什么它不会抛出导入错误。 And if it wasn't finding it, I don't understand how the scope was different.如果它没有找到它,我不明白范围有什么不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM