自定义pytest参数化测试名称

时间:2021-03-15 02:18:32

标签: python python-3.x pytest parameterized-unit-test

我进行了以下测试:

@pytest.mark.parametrize(
    "nums",
    [[3, 1, 5, 4, 2], [2, 6, 4, 3, 1, 5], [1, 5, 6, 4, 3, 2]]
)
def test_cyclic_sort(nums):
    pass


@pytest.mark.parametrize(
    "nums, missing",
    [([4, 0, 3, 1], 2)]
)
def test_find_missing_number(nums, missing):
    pass

我想自定义测试名称以包含输入数组。我已阅读 pytest docsthis questionthis question,但没有人回答以下问题:

  1. 传递给 id func 的是什么?在我上面的代码中,第一个测试需要一个参数,第二个需要两个。
  2. pytest 文档使用顶级函数作为 id,而我想将我的测试放在一个类中并使用 @staticmethod。尝试从 TestClass.static_method 内部使用 TestClass 引用静态方法会在 PyCharm 中出现错误;这样做的正确语法是什么?

编辑: 已创建 https://github.com/pytest-dev/pytest/issues/8448

1 个答案:

答案 0 :(得分:1)

当对 ids 关键字使用可调用对象时,它将使用单个参数调用:被参数化的测试参数的值。可调用的 ids 返回一个字符串,该字符串将在方括号中用作测试名称后缀。

如果测试参数化多个值,该函数仍将使用单个参数调用,但每个测试将调用多次。生成的名称将与破折号连接,类似于

"-".join([idfunc(val) for val in parameters])

例如:

test_something[val1-val2-val3]

Here is the join in the pytest source

要使用静态方法,此语法有效:

class TestExample:

    @staticmethod
    def idfunc(val):
        return f"foo{val}"

    @pytest.mark.parametrize(
        "x, y",
        [
            [1, 2],
            ["a", "b"],
        ],
        ids=idfunc.__func__,
    )
    def test_vals(self, x, y):
        assert x
        assert y

这将生成两个测试,如上所述调用 idfunc 四次。

TestExample::test_vals[foo1-foo2]
TestExample::test_vals[fooa-foob]