ValueError 在 pytest 中不使用参数,装饰器的顺序重要吗?



我在pytest中遇到了一个非常神秘的错误,在添加"@pytest.mark.parametrize"装饰器后,测试开始抛出以下错误:

ValueError: <function ... at ...> uses no argument 'parameters'

我在这里找到了错误的源

以下是我的函数的签名(简化(:

@patch('dog')
@pytest.mark.parametrize('foo,bar', test_data)
def test_update_activity_details_trainer_and_gear(self, foo, bar, dog):

原来是 pytest 事务中装饰器的顺序

@pytest.mark.parametrize('foo,bar', test_data)
@patch('dog')
def test_update_activity_details_trainer_and_gear(self, dog, foo, bar):

更改顺序删除了错误

对于类似的用例,我有相同的错误消息。 发生此错误是因为我省略了将mock作为测试函数中的第一个关键字参数。

这有效:

import unittest.mock as mock
@pytest.mark.parametrize('foo,bar', test_data)
@mock.patch('module.dog.Dog.__init__', return_value=None)
def test_update_activity_details_trainer_and_gear(_: mock.MagicMock, foo, bar):

此错误为:In test_update_activity_details_trainer_and_gear: function uses no argument 'foo'

import unittest.mock as mock
@pytest.mark.parametrize('foo,bar', test_data)
@mock.patch('module.dog.Dog.__init__', return_value=None)
def test_update_activity_details_trainer_and_gear(foo, bar):

最新更新