一、pytest实现参数化(@pytest.mark.parametrize)
@pytest.mark. parametrize装饰器可以实现对测试用例的参数化,方便测试数据的获取。
@pytest.mark. parametrize的基本使用:
方便测试函数对测试数据的获取。 方法: parametrize(argnames, argvalues, indirect=False, ids=None, scope=None) 常用参数: argnames:参数名 argvalues:参数对应值,类型必须为list 当参数为一个时格式:[value] 当参数个数大于一个时,格式为:[(param_value1,param_value2.....),(param_value1,param_value2.....)] 使用方法: @pytest.mark.parametrize(argnames,argvalues)
单个参数【测试函数入参只有一个参数】
# file_name: test_parametrize.py import pytest class Test_D: @pytest.mark.parametrize("a", [1, 2, 3]) # 参数a被赋予3个值,test_a将会运行3遍 def test_a(self, a): # 参数必须和parametrize里面的参数一致 print(\'\n------------------> test_a has ran, and a = {}\'.format(a)) assert 1 == a if __name__ == \'__main__\': pytest.main([\'-s\', \'test_parametrize.py\'])
运行结果:
从运行结果中可以看到test_a方法被执行了3遍,说明参数a参数化成功。
多个参数【测试函数入参有多个参数】
# file_name:test_parametrize.py import pytest class Test_D: @pytest.mark.parametrize("a,b", [(1, 2), (2, 3), (3, 4)]) # 参数a,b均被赋予三个值,函数会运行三遍 def test_b(self, a, b): # 参数必须和parametrize里面的参数一致 print(\'\n------------------> test_b has ran, and a = {}, b = {}\'.format(a, b)) assert 1 if __name__ == \'__main__\': pytest.main([\'-s\', \'test_parametrize.py\'])
运行结果:
从运行结果中可以看到test_b方法被执行了三遍,说明参数a,b都已经被参数化。
利用函数的返回值进行参数化
# file_name: test_parametrize.py import pytest # 定义返回参数值的函数 def return_test_data(): return [(1, 2), (2, 3), (3, 4)] class Test_D: @pytest.mark.parametrize("a,b", return_test_data()) # 使用函数返回值的方式传入参数值 def test_c(self, a, b): print(\'\n------------------> test_c has ran, and a = {}, b = {}\'.format(a, b)) assert 1 if __name__ == \'__main__\': pytest.main([\'-s\', \'test_parametrize.py\'])
运行结果:
从运行结果中可以看到test_c方法被执行了三遍,这说明使用函数的返回值方式同样可以做到参数化。
参数组合
获得多个参数化参数的所有组合,可以堆叠参数化装饰器:
# file_name: test_parametrize.py import pytest class Test_D: @pytest.mark.parametrize("x", [1, 2]) @pytest.mark.parametrize("y", [3, 4]) def test_d(self, x, y): print("\n------------------> test_d has ran, and x={}, y={}".format(x, y)) if __name__ == \'__main__\': pytest.main([\'-s\', \'test_parametrize.py\'])
运行结果:
从运行结果中可以看到test_d方法被执行了四遍,这是因为参数x和参数y分别被赋予了2个值,组合起来就是2*2=4次,说明装饰器叠加可以获得所有参数的组合。
二、参数化parameter加mark标记
pytest 使用 parametrize 参数化的时候,有多组测试数据,需要对其中的一些测试数据加标记跳过,可以用pytest.param实现。
pytest.param
先看下 pytest.param 源码,可以传三个参数:
- param values :按顺序传参数集值的变量args
- keyword marks : marks关键字参数,要应用于此参数集的单个标记或标记列表。
- keyword str id: id字符串关键字参数,测试用例的id属性
def param(*values, **kw): """Specify a parameter in `pytest.mark.parametrize`_ calls or :ref:`parametrized fixtures <fixture-parametrize-marks>`. .. code-block:: python @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), pytest.param("6*9", 42, marks=pytest.mark.xfail), ]) def test_eval(test_input, expected): assert eval(test_input) == expected :param values: variable args of the values of the parameter set, in order. :keyword marks: a single mark or a list of marks to be applied to this parameter set. :keyword str id: the id to attribute to this parameter set. """ return ParameterSet.param(*values, **kw)
使用示例:
import pytest @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), pytest.param("6*9", 42, marks=pytest.mark.xfail), ]) def test_eval(test_input, expected): assert eval(test_input) == expected
运行结果:
skip跳过用例
1、上面的案例是标记xfail,想标记skip跳过用例也是可以的
import pytest @pytest.mark.parametrize("user,psw", [("yoyo1", "123456"), ("yoyo2", "123456"), pytest.param("yoyo3", "123456", marks=pytest.mark.skip)]) def test_login(user, psw): print(user + " : " + psw) assert 1 == 1
运行结果:
2、上面的1个参数可以用pytest.param格式进行参数化:
import pytest @pytest.mark.parametrize("number", [pytest.param("1"), pytest.param("2"), pytest.param("3", marks=pytest.mark.skip)]) def test_login1(number): print(number) assert 1 == 1
运行结果:
3、上面的2个参数也可以用pytest.param格式进行参数化:
import pytest @pytest.mark.parametrize("user,psw", [pytest.param("yoyo1", "123456"), pytest.param("yoyo2", "123456"), pytest.param("yoyo3", "123456", marks=pytest.mark.skip)]) def test_login1(user, psw): print(user + " : " + psw) assert 1 == 1
运行结果:
id参数
id参数是给用例添加标题内容,没加id参数的时候,用例会默认拿请求的参数当用例标题:
添加id参数:
import pytest @pytest.mark.parametrize("user,psw", [pytest.param("yoyo1", "123456", id="test case1: yoyo1"), pytest.param("yoyo2", "123456", id="test case2: yoyo2"), pytest.param("yoyo3", "123456", marks=pytest.mark.skip, id="test case3: yoyo3")]) def test_login1(user, psw): print(user + " : " + psw) assert 1 == 1
运行结果: