python data_error ai_generated true

值错误:向 'test_add' 传递了 2 个参数,但预期 1 个

ValueError: 2 parameters passed to 'test_add' but expected 1

ID: python/pytest-parametrize-arg-count-mismatch

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-11-12首次发现

版本兼容性

版本状态引入弃用备注
3.10 active

根因分析

@pytest.mark.parametrize 装饰器提供的参数数量与测试函数的参数列表不匹配,导致 ValueError。

English

The @pytest.mark.parametrize decorator provides a number of arguments that does not match the test function's parameter list, causing a ValueError.

generic

解决方案

  1. 95% 成功率
    Ensure the number of arguments in the parametrize decorator matches the test function's parameters. For example, if the test expects (a, b), use @pytest.mark.parametrize('a,b', [(1,2)])
  2. 80% 成功率
    Use a single argument with a tuple or list if the test function accepts one parameter that contains multiple values.

无效尝试

常见但无效的做法:

  1. 50% 失败

    Adding extra parameters to the test function without updating the parametrize decorator may fix the count but could break other tests.

  2. 70% 失败

    Removing arguments from the decorator without adjusting the test function leads to missing required parameters.