Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 重用补丁语句_Python_Python 3.x_Unit Testing_Pytest - Fatal编程技术网

Python 重用补丁语句

Python 重用补丁语句,python,python-3.x,unit-testing,pytest,Python,Python 3.x,Unit Testing,Pytest,我在很多单元测试中都重复了这两条语句 with patch('/path') as mocked_thing1: mocked_thing1.return_value = 'some value' with patch('/path') as mocked_thing2: mocked_thing2.return_value = 'some value' 有没有一种方法可以让我为它编写一个函数,然后

我在很多单元测试中都重复了这两条语句

        with patch('/path') as mocked_thing1:
            mocked_thing1.return_value = 'some value'
            with patch('/path') as mocked_thing2:
                mocked_thing2.return_value = 'some value'

有没有一种方法可以让我为它编写一个函数,然后直接调用它,而不是在我的测试用例中重复编写这些行?

如果要更改不同测试的返回值,只能在每个测试中分配返回值。一种方法是为模拟添加单独的装置:

@pytest.fixture
def mock_thing1():
将修补程序('/path')作为模拟内容1:
屈服嘲弄的东西1
@pytest.fixture
def mock_thing2():
将补丁('/path')作为模拟内容2:
屈服嘲弄的东西2
def测试内容(模拟内容1、模拟内容2):
mocked_thing1.return_value='some value'
mocked_thing2.return_value='some value'
...
另一种可能是使用间接夹具参数设置夹具中的返回值:

@pytest.fixture
def模拟内容1(请求):
将修补程序('/path')作为模拟内容1:
mocked_thing1.return_value=request.param
屈服嘲弄的东西1
@pytest.fixture
def模拟内容2(请求):
将补丁('/path')作为模拟内容2:
mocked_thing2.return_value=request.param
屈服嘲弄的东西2
@pytest.mark.parametrize(“mock_thing2”,['some other value',indirect=True)
@pytest.mark.parametize(“mock_thing1”,['some value',indirect=True)
def测试内容(模拟内容1、模拟内容2):
...
或者您可以只使用
patch
的decorator版本而不是任何fixture,这样至少测试主体的可读性更好:

@mock.patch(“mock\u thing2”,返回\u value='some other value')
@补丁(“mock\u thing1”,返回值='some value')
def测试内容(模拟内容1、模拟内容2):
...

如果要更改不同测试的返回值,则只能在每个测试中指定返回值。一种方法是为模拟添加单独的装置:

@pytest.fixture
def mock_thing1():
将修补程序('/path')作为模拟内容1:
屈服嘲弄的东西1
@pytest.fixture
def mock_thing2():
将补丁('/path')作为模拟内容2:
屈服嘲弄的东西2
def测试内容(模拟内容1、模拟内容2):
mocked_thing1.return_value='some value'
mocked_thing2.return_value='some value'
...
另一种可能是使用间接夹具参数设置夹具中的返回值:

@pytest.fixture
def模拟内容1(请求):
将修补程序('/path')作为模拟内容1:
mocked_thing1.return_value=request.param
屈服嘲弄的东西1
@pytest.fixture
def模拟内容2(请求):
将补丁('/path')作为模拟内容2:
mocked_thing2.return_value=request.param
屈服嘲弄的东西2
@pytest.mark.parametrize(“mock_thing2”,['some other value',indirect=True)
@pytest.mark.parametize(“mock_thing1”,['some value',indirect=True)
def测试内容(模拟内容1、模拟内容2):
...
或者您可以只使用
patch
的decorator版本而不是任何fixture,这样至少测试主体的可读性更好:

@mock.patch(“mock\u thing2”,返回\u value='some other value')
@补丁(“mock\u thing1”,返回值='some value')
def测试内容(模拟内容1、模拟内容2):
...

您可以使用这些行创建一个函数,然后在每次测试中导入它,但我看到路径总是在变化!?pytest固定装置会有所帮助。固定装置当然是最好的选择。如何做到这一点取决于:路径和“某个值”是否始终相同?您是否需要在测试之后访问模拟对象?“某些值”将更改,但路径将保持不变。之后我不需要访问模拟对象。这些行并不总是需要在每个测试用例开始时执行。事实上,在某些情况下,我需要先生成这个“some value”,然后将mock返回值设置为这个“some value”。您可以使用这些行创建一个函数,然后在每个测试中导入它,但我看到路径总是在变化!?pytest固定装置会有所帮助。固定装置当然是最好的选择。如何做到这一点取决于:路径和“某个值”是否始终相同?您是否需要在测试之后访问模拟对象?“某些值”将更改,但路径将保持不变。之后我不需要访问模拟对象。这些行并不总是需要在每个测试用例开始时执行。事实上,在某些情况下,我需要首先生成这个“somevalue”,然后mocked返回值应该设置为这个“somevalue”。