Skip to content

Latest commit

 

History

History
80 lines (64 loc) · 1.92 KB

PT007.md

File metadata and controls

80 lines (64 loc) · 1.92 KB

PT007

wrong values type in @pytest.mark.parametrize, expected {expected_type}

The expected type of the list of rows is controlled by the configuration variable pytest-parametrize-values-type. The expected type of each row in case of multiple arguments is controlled by the configuration variable pytest-parametrize-values-row-type.

Configuration

  • pytest-parametrize-values-type
    Expected type for the list of values rows in @pytest.mark.parametrize. The following values are supported:

    • tuple — e.g. @pytest.mark.parametrize('name', (1, 2, 3))
    • list (default) — e.g. @pytest.mark.parametrize('name', [1, 2, 3])
  • pytest-parametrize-values-row-type
    Expected type for each row of values in @pytest.mark.parametrize in case of multiple parameters. The following values are supported:

    • tuple (default) — e.g. @pytest.mark.parametrize(('name1', 'name2'), [(1, 2), (3, 4)])
    • list — e.g. @pytest.mark.parametrize(('name1', 'name2'), [[1, 2], [3, 4]])

Examples

Bad code (assuming pytest-parametrize-values-type set to list and pytest-parametrize-values-row-type set to tuple):

import pytest

# expected list, got tuple
@pytest.mark.parametrize('param', (1, 2))
def test_foo(param):
    ...

# expected top-level list, got tuple
@pytest.mark.parametrize(
    ('param1', 'param2'),
    (
        (1, 2),
        (3, 4),
    ),
)
def test_bar(param1, param2):
    ...

# expected individual rows to be tuples, got lists
@pytest.mark.parametrize(
    ('param1', 'param2'),
    [
        [1, 2],
        [3, 4],
    ],
)
def test_baz(param1, param2):
    ...
import pytest

@pytest.mark.parametrize('param', [1, 2])
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