跳到内容

pytest-parametrize-names-wrong-type (PT006)

源自 flake8-pytest-style linter。

有时提供修复。

作用

检查传递给 pytest.mark.parametrize 的参数名称的类型。

为什么这不好?

pytest.mark.parametrizeargnames 参数接受字符串或字符串序列。对于单个参数,最好使用字符串。对于多个参数,最好使用通过 lint.flake8-pytest-style.parametrize-names-type 设置配置的样式。

示例

import pytest


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


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


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

建议改为

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): ...

Options (选项)

参考