python config_error ai_generated true

In test_math.py: @pytest.mark.parametrize argument names ['a', 'b'] do not match the test function signature ['x', 'y']

ID: python/pytest-parametrize-marks-mismatch

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-08-09First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

The parametrize argnames string does not match the test function's parameter names. pytest requires exact names (order matters for tuple args).

generic

中文

parametrize 的 argnames 字符串与测试函数的参数名不匹配。pytest 要求名称完全一致(对元组参数而言顺序也重要)。

Workarounds

  1. 98% success
    Match names exactly:
      @pytest.mark.parametrize('x,y', [(1,2),(3,4)])
      def test_add(x, y): ...
    Or use a list of names:
      @pytest.mark.parametrize(['x','y'], [...])
  2. 95% success
    Use pytest.param with ids for clarity:
      @pytest.mark.parametrize('x,y', [pytest.param(1,2,id='small')])

Dead Ends

Common approaches that don't work:

  1. 90% fail

    pytest inspects the signature and rejects **kwargs for parametrization; test still errors.

  2. 80% fail

    pytest treats 'args' as a single parameter, breaking tuple unpacking.