接口自动化的ymal数据驱动:把数据从用例中抽取出来

苹果芒 / 2023-05-06 / 原文

接口自动化的数据驱动是指:把数据从用例中抽取出来。

一.接口自动化测试框架Ymal数据驱动的封装

@pytest.mark.parameterize(args_name,args_value)

args_name:参数名

args_valus:参数值(可使用:list列表,tuple元组,字典列表,字段元组等)在数据中有多少个值,那么接口用例就会被执行多少次。

ps: args_valus可以是填入的值,也可以是一个有返回值的函数。

案例1 : list列表

import pytest

class TestDemo3:
    @pytest.mark.parametrize("args",["张山","李四"])
    def test_getInfo(self,args):
        print(args)
if __name__ == '__main__':
    pytest("TestDemo2_test.py")

 result: 

TestDemo3_test.py::TestDemo3::test_getInfo[\u5f20\u5c71] PASSED          [ 50%]张山

TestDemo3_test.py::TestDemo3::test_getInfo[\u674e\u56db] PASSED          [100%]李四

 

案例2: 值是list中嵌套list

import pytest
class TestDemo3:
    @pytest.mark.parametrize("args",[["张山",13],["李四",16]])
    def test_getInfo(self,args):
        print(args)
if __name__ == '__main__':
    pytest("TestDemo2_test.py")

 result:

TestDemo3_test.py::TestDemo3::test_getInfo[args0] PASSED                 [ 50%]['张山', 13]

TestDemo3_test.py::TestDemo3::test_getInfo[args1] PASSED                 [100%]['李四', 16]

 

案例3:当value是嵌套的list或者元组时,可以用多个参数来接收--这称之为“解包”

class TestDemo3:
    @pytest.mark.parametrize("name,age",[["张山",13],["李四",16]])
    def test_getInfo(self,name,age):
        print(name,age)
if __name__ == '__main__':
    pytest("TestDemo2_test.py")

result:

TestDemo3_test.py::TestDemo3::test_getInfo[\u5f20\u5c71-13] PASSED       [ 50%]张山 13

TestDemo3_test.py::TestDemo3::test_getInfo[\u674e\u56db-16] PASSED       [100%]李四 16

二. Ymal详解

ymal也是一种数据格式。

ymal主要作用:

1.做 配置文件

2.做 测试用例的数据

ymal数据组成:

1.map对象:键值对格式 ,键(冒号)值

name: 张三

2.列表: 用横向-开头

  names:
     -name1:张山
     -name2:李四

ps:

1.如果是列表的值,就用横线-表示

2.如果有多组数据,也用横线-分隔