Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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 如何在使用mark.parametrize decorator时使用修补程序decorator?_Python_Unit Testing_Pytest_Python Unittest_Patch - Fatal编程技术网

Python 如何在使用mark.parametrize decorator时使用修补程序decorator?

Python 如何在使用mark.parametrize decorator时使用修补程序decorator?,python,unit-testing,pytest,python-unittest,patch,Python,Unit Testing,Pytest,Python Unittest,Patch,我有一个测试函数,可以修补一些东西。当我遇到超过2或3个补丁时,功能开始变得非常糟糕 def test_bla(): with patch(...): with patch(...): with patch(...): # test 所以我试着开始使用装饰器。下面是我的示例测试,当我引入补丁装饰器时,它会中断 import pytest from unittest.mock import patch class

我有一个测试函数,可以修补一些东西。当我遇到超过2或3个补丁时,功能开始变得非常糟糕

def test_bla():
    with patch(...):
        with patch(...):
            with patch(...):
                # test
所以我试着开始使用装饰器。下面是我的示例测试,当我引入补丁装饰器时,它会中断

import pytest
from unittest.mock import patch

class Example:
    def _run(self, x):
        return x

    def run(self, x):
        return self._run(x) + 1


@pytest.mark.parametrize('x', [1])
def test_example(x):
    assert Example().run(x) == 2


@pytest.mark.parametrize('x', [1])
@patch('Example._run', return_value=0)
def test_example_failure(x):
    with pytest.raises(AssertionError):
        assert Example().run(x) == 2
这是我得到的错误:

platform linux -- Python 3.6.13, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: ..., configfile: pytest.ini
plugins: metadata-1.11.0
collected 0 items / 1 error                                                                                                                                                                                    

==================================================================================================== ERRORS ====================================================================================================
________________________________________________________________________________________ ERROR collecting test_patch.py ________________________________________________________________________________________
In test_example_failure: function uses no argument 'x'
=========================================================================================== short test summary info ============================================================================================
ERROR test_patch.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================================================================== 1 error in 0.12s ===============================================================================================
我试着改变装饰者的顺序(正如我在一些SO答案中看到的那样),但那没有帮助


我刚刚意识到的另一件事是,使用测试参数而不使用参数化是可行的!(我对代码做了一些调整,因为我无法从自己的模块中修补类)

导入pytest
随机输入
从unittest.mock导入修补程序
课堂示例:
def_运行(自我,x):
返回random.randint(0,6)
def运行(自我,x):
返回自运行(x)+1
@pytest.mark.parametrize('x',[1])
def测试_示例(x):
断言示例()。运行(x)<7
#@pytest.mark.parametrize('x',[1])
@补丁('random.randint',返回值=0)

def test_示例_失败(x):#
@patch
正在向测试对象
MagicMoc
发送一个参数。如果要从
@pytest.mark.parametrize
发送另一个参数,则需要向测试添加另一个参数

@pytest.mark.parametrize('x', [1])
@patch('Example._run', return_value=0)
def test_example_failure(mocked_class, x):
    with pytest.raises(AssertionError):
        assert Example().run(x) == 2

# randint = <MagicMock name='randint' id='2158546057008'>
# x = 1
@pytest.mark.parametrize('x',[1])
@修补程序('Example.\u run',return\u value=0)
def测试失败示例(模拟类,x):
使用pytest.raises(断言错误):
断言示例()。运行(x)=2
#randint=
#x=1

这与使用上下文修补某些东西不同吗?i、 e.
带补丁(bla):foo()
?我最终想用patch()替换大量的
行,用
@patch()
lines@CIsForCookies我不太熟悉
补丁
,但据我所知;同样的功能,可能是set
mocked\u class.side\u effect=some\u function
?@CIsForCookies还有一个用于mock的库,带有
pytest
,它看起来是一个不错的包,但实际上大部分都与unittest mock非常相似,至少从我的需求来看是这样的。
@pytest.mark.parametrize('x', [1])
@patch('Example._run', return_value=0)
def test_example_failure(mocked_class, x):
    with pytest.raises(AssertionError):
        assert Example().run(x) == 2

# randint = <MagicMock name='randint' id='2158546057008'>
# x = 1