Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python @修补程序装饰器与pytest fixture不兼容_Python_Mocking_Pytest_Fixtures - Fatal编程技术网

Python @修补程序装饰器与pytest fixture不兼容

Python @修补程序装饰器与pytest fixture不兼容,python,mocking,pytest,fixtures,Python,Mocking,Pytest,Fixtures,在使用与pytest fixture集成的mock包中的补丁装饰器时,我遇到了一些神秘的事情 我有两个模块: -----test folder -------func.py -------test_test.py 在func.py中: def a(): return 1 def b(): return a() 在test_test.py中: import pytest

在使用与pytest fixture集成的mock包中的补丁装饰器时,我遇到了一些神秘的事情

我有两个模块:

    -----test folder
          -------func.py
          -------test_test.py
在func.py中:

    def a():
        return 1

    def b():
        return a()     
在test_test.py中:

    import pytest
    from func import a,b
    from mock import patch,Mock

    @pytest.fixture(scope="module")
    def brands():
        return 1


    mock_b=Mock()

    @patch('test_test.b',mock_b)
    def test_compute_scores(brands):                 
         a()

看来补丁装饰与pytest夹具不兼容。有人对此有见解吗?谢谢

我遇到了同样的问题,解决方案是在1.0.1版本中使用mock库(在2.6.0版本中使用unittest.mock之前)。现在它就像一个符咒:)

这并不能直接解决您的问题,但有一个插件可以让您编写以下内容:

def test_compute_scores(brands, mock):                 
     mock_b = mock.patch('test_test.b')
     a()

希望这个关于一个老问题的答案能对某人有所帮助

首先,问题不包括错误,所以我们不知道到底发生了什么。但我会尽力提供一些帮助我的东西

如果您希望用修补过的对象装饰测试,那么为了让它与pytest一起工作,您可以执行以下操作:

@mock.patch('mocked.module')
def test_me(*args):
    mocked_module = args[0]
或对于多个修补程序:

@mock.patch('mocked.module1')
@mock.patch('mocked.module')
def test_me(*args):
    mocked_module1, mocked_module2 = args
pytest正在寻找要在测试函数/方法中查找的装置的名称。提供
*args
参数可以很好地解决查找阶段的问题。因此,要包含带有面片的装置,可以执行以下操作:

# from question
@pytest.fixture(scope="module")
def brands():
    return 1

@mock.patch('mocked.module1')
def test_me(brands, *args):
    mocked_module1 = args[0]
这对我运行python 3.6和pytest 3.0.6很有效。

起,
mock
模块已被拉入
unittest
库。还有一个后台端口(用于Python的早期版本)作为独立库
mock
提供

在同一测试套件中组合这两个库会产生上述错误:

E       fixture 'fixture_name' not found
在测试套件的虚拟环境中,运行
pip uninstall mock
,确保您没有在核心unittest库旁边使用后端口库。卸载后重新运行测试时,如果是这种情况,您将看到
ImportError
s


将此导入的所有实例替换为来自unittest.mock导入的

当将pytest
夹具
mock.patch
一起使用时,测试参数顺序至关重要

如果将夹具参数放置在模拟参数之前:

from unittest import mock

@mock.patch('my.module.my.class')
def test_my_code(my_fixture, mocked_class):
然后模拟对象将位于
my_fixture
中,并且
mock_类
将作为一个fixture进行搜索:

fixture 'mocked_class' not found
但是,如果颠倒顺序,请将fixture参数放置在末尾:

from unittest import mock

@mock.patch('my.module.my.class')
def test_my_code(mocked_class, my_fixture):

然后一切都会好起来。

如果要应用多个修补程序,则它们的注入顺序很重要:

#来自问题
@pytest.夹具(scope=“模块”)
def brands():
返回1
#注意命令
@补丁('my.module.my.class1')
@补丁('my.module.my.class2')
def测试列表实例elb tg(模拟2类、模拟1类、品牌):
通过

谢谢您的回答。我在使用mock library 1.0.1时遇到了这个问题。这个问题是关于在方法签名中混合pytest装置和补丁,而不仅仅是补丁。我添加了关于装置的部分,完成了问题的答案。我想说这有点老套,但它很管用。感谢@zalpha314指出这一点。我有一个类似的问题,我从unittest.mock导入补丁导入
,以及
import mock
,我必须删除import mock语句,然后停止抛出
fixture'mocked_instance'not found
错误。我建议您切换接受的答案。非常感谢。这解决了我的问题:)这是解决方案,而不是康拉德的方案。这应该是解决方案