Skip to content

Latest commit

 

History

History
57 lines (41 loc) · 1.49 KB

PT006.md

File metadata and controls

57 lines (41 loc) · 1.49 KB

PT006

wrong name(s) type in @pytest.mark.parametrize, expected {expected_type}

For a single name the expected type is always a plain string. For multiple names the expected type is controlled by the configuration variable pytest-parametrize-names-type.

Configuration

  • pytest-parametrize-names-type
    Expected type for multiple argument names in @pytest.mark.parametrize. The following values are supported:
    • csv — a comma-separated list, e.g. @pytest.mark.parametrize('name1,name2', ...)
    • tuple (default) — e.g. @pytest.mark.parametrize(('name1', 'name2'), ...)
    • list — e.g. @pytest.mark.parametrize(['name1', 'name2'], ...)

Examples

Bad code (assuming pytest-parametrize-names-type is set to tuple):

import pytest

# single parameter, always expecting string
@pytest.mark.parametrize(('param', ), [1, 2, 3])
def test_foo(param):
    ...

# multiple parameters, expecting tuple due to settings
@pytest.mark.parametrize(['param1', 'param2'], [(1, 2), (3, 4)])
def test_bar(param1, param2):
    ...

# multiple parameters, expecting tuple due to settings
@pytest.mark.parametrize('param1,param2', [(1, 2), (3, 4)])
def test_baz(param1, param2):
    ...

Good code:

import pytest

@pytest.mark.parametrize('param', [1, 2, 3])
def test_foo(param):
    ...

@pytest.mark.parametrize(('param1', 'param2'), [(1, 2), (3, 4)])
def test_bar(param1, param2):
    ...

Rationale

  • to enforce consistency between all tests in a codebase