如何修复 "PT006 wrong name(s) type in @pytest.mark.parametrize, expected tuple"?

How to fix "PT006 wrong name(s) type in @pytest.mark.parametrize, expected tuple"?

当我 运行 flake8 我得到以下错误:

./test_sample.py:4:2: PT006 wrong name(s) type in @pytest.mark.parametrize, expected tuple

这是我的代码

import pytest


@pytest.mark.parametrize('foo,bar', [(1, 1), (2, 2)])
def test_foo(foo, bar):
    assert foo == bar

pip freeze | egrep flake8:

flake8==3.8.4
flake8-plugin-utils==1.3.1
flake8-pytest-style==1.3.0

如何修复错误?

此错误由 flake8-pytest-style 生成。参见 https://github.com/m-burst/flake8-pytest-style/blob/master/docs/rules/PT006.md

你有两个选择:

  • 'foo,bar'替换为('foo', 'bar')
    import pytest
    
    
    @pytest.mark.parametrize(('foo', 'bar'), [(1, 1), (2, 2)])
    def test_foo(foo, bar):
        assert foo == bar
    
  • 通过将包含以下内容的名为 .flake8 的文件添加到项目的根目录来更改 pytest-parametrize-names-type 配置选项:
    [flake8]
    pytest-parametrize-names-type = csv