pytest + yaml 框架 -24.单个用例中参数化功能实现

发布时间 2023-04-14 10:11:22作者: 上海-悠悠

前言

早期版本参数化功能实现只支持在config 中全局的地方写parameters 关键字,只实现了基本的功能。
v1.2.1 版本对parameters 参数化功能做了进一步的细分,支持在case 用例中针对单个用例的参数化了。

parameters 参数化

用例参数化的实现,我设计了2种实现方式

参数化方式1:

  config:
     name: post示例
     fixtures: username, password
     parameters:
       - [test1, '123456']
       - [test2, '123456']

参数化方式2:

  config:
     name: post示例
     parameters:
       - {"username": "test1", "password": "123456"}
       - {"username": "test2", "password": "1234562"}

使用 fixtures 功能实现参数化

基本实现原理参考 pytest 框架的参数化实现

import pytest
@pytest.mark.parametrize("test_input,expected",
                         [ ["3+5", 8],
                           ["2+4", 6],
                           ["6 * 9", 42]
                         ])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

在上面的用例中,只需要关注参数化的2个变量test_input, expected 也就是在测试用例中传的2个值,可以理解为用例的2个fixture参数
还需要关注测试数据,测试数据结构必须是list,里面是可以迭代的数据,因为有2个变量,所以每组数据是2个值。

在yaml文件中

  • 参数化需要的变量写到config的fixtures 位置
  • 参数化需要的数据写到 parameters

示例
test_params.yml

# 作者-上海悠悠 微信/QQ交流:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/

config:
  name: post示例
  fixtures: username, password
  parameters:
    - [test1, '123456']
    - [test2, '123456']

teststeps:
-
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: ${username}
      password: ${password}
  extract:
      url:  body.url
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$..username, '${username}']
    - eq: [body.json.username, '${username}']

运行yaml文件

pytest test_params.yml

会自动生成2个测试用例

parameters 实现参数化

第二种实现方式,可以不用在fixtures 中传变量,但是 parameters 测试数据必须是字典类型,从字典的 key 中动态读取变量名称
test_params_2.yml

# 作者-上海悠悠 微信/QQ交流:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/
config:
  name: post示例
  parameters:
    - {"username": "tes1", "password": "123456"}
    - {"username": "tes2", "password": "123456"}

teststeps:
-
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: ${username}
      password: ${password}
  extract:
      url:  body.url
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$..username, '${username}']
    - eq: [body.json.username, '${username}']

运行yaml文件

pytest test_params.yml

以上2种实现参数化方式效果是一样的

用例中添加参数化

config中添加参数化,只适合当前yaml 文件中只有一个用例的情况,如果yaml中有多个用例,会对每个用例都参数化了。
现在新的v1.2.1版本,可以支持针对单个用例的参数化实现

如下用例,会生成7个用例

config:
  name: post示例


teststeps:
  name: xx
  print: "xx"

teststeps2:
  name: xx
  fixtures: name
  parameters: [1, 2, 3]
  print: ${name}

test_2:
  name: yy
  print: "yy"

test_3:
  name: yy
  parameters:
    - {"x": 1, "y": 2}
    - {"x": 2, "y": 3}
  print: ${x}--${y}

如果 用例是包含有多个步骤的情况,那么参数化写到步骤中的第一个步骤即可(写到后面步骤无法识别)

config:
  name: post示例


test_steps:
-
  name: step1
  fixtures: name
  parameters: [1, 2, 3]
  print: ${name}
-
  name: step2
  print: "xx"


test_2:
-
  name: yy
  print: "yy"

test_3:
-
  name: yy
  parameters:
    - {"x": 1, "y": 2}
    - {"x": 2, "y": 3}
  print: ${x}--${y}

函数返回参数化数据

参数化的数据,也可以通过函数来获取数据源

conftest.py 文件

from pytest_yaml_yoyo import my_builtins


def params():
    """返回参数化需要的数据"""
    return [
        {"username": "tes1", "password": "123456"},
        {"username": "tes2", "password": "123456"}
    ]

my_builtins.params = params

test_parms.yml 文件内容

config:
  name: 参数化引用函数示例
  parameters: ${params()}

teststeps:
-
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: ${username}
      password: ${password}
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$..username, '${username}']
    - eq: [body.json.username, '${username}']

parameters 数据源必须是符合参数化的格式。