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

ssh*_*gin 6 python pytest

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

ValueError: <function ... at ...> uses no argument 'parameters'
Run Code Online (Sandbox Code Playgroud)

我在这里找到了错误的来源

这是我的函数签名的样子(简化):

@patch('dog')
@pytest.mark.parametrize('foo,bar', test_data)
def test_update_activity_details_trainer_and_gear(self, foo, bar, dog):
Run Code Online (Sandbox Code Playgroud)

ssh*_*gin 9

事实证明 pytest 中装饰器的顺序很重要

@pytest.mark.parametrize('foo,bar', test_data)
@patch('dog')
def test_update_activity_details_trainer_and_gear(self, dog, foo, bar):
Run Code Online (Sandbox Code Playgroud)

更改顺序消除了错误

  • 当然。装饰器只是一种语法糖。就像 `a(b(c()))` 可能与 `c(b(a()))` 不同 (5认同)