pytest简易教程(13):parametrize参数化

pytest简易教程汇总,详见:https://www.cnblogs.com/uncleyong/p/17982846

关于parametrize参数化

之前我们分享了通过fixture返回值实现参数化(详见:https://www.cnblogs.com/uncleyong/p/17957896)

今天我们分享parametrize参数化,也就是在测试函数/测试类进行参数化

parametrize是一个内置标记,在命令pytest --markers结果中可以看到@pytest.mark.parametrize(argnames, argvalues)

源码

    class _ParametrizeMarkDecorator(MarkDecorator):
        def __call__(  # type: ignore[override]
            self,
            argnames: Union[str, Sequence[str]],
            argvalues: Iterable[Union[ParameterSet, Sequence[object], object]],
            *,
            indirect: Union[bool, Sequence[str]] = ...,
            ids: Optional[
                Union[
                    Iterable[Union[None, str, float, int, bool]],
                    Callable[[Any], Optional[object]],
                ]
            ] = ...,
            scope: Optional[_ScopeName] = ...,
        ) -> MarkDecorator:
            ...


方法:parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)

常用参数:

  • argnames:参数名,格式为:"arg1,arg2,arg3,...",通过逗号分隔多个参数
多参数写法汇总:参数名可以是字符串、元组、列表、字符串放元组中

@pytest.mark.parametrize("input,expected", [("1+1", 2), ("2-4", -2), ("2*3", 6)])

@pytest.mark.parametrize(("input","expected"), [("1+1", 2), ("2-4", -2), ("2*3", 6)])

@pytest.mark.parametrize(["input","expected"], [("1+1", 2), ("2-4", -2), ("2*3", 6)])

@pytest.mark.parametrize(("input,expected"), [("1+1", 2), ("2-4", -2), ("2*3", 6)])

你可能感兴趣的:(pytest)