📝编写YAML自动生成用例

写在前面:该feature是在v1.0提供的探索性功能,在v2.0中已经不推荐使用了,如果对这个feature感兴趣或对你产生了灵感,可以扒下来二次开发

aomaker提供了一套YAML模板语法,只要遵循规定语法,就可以不用写代码,直接通过YAML文件直接翻译为pytest风格的代码文件。

YAML语法介绍示例:

  • testcase_class_name(必填):指定测试类名称
  • description(可选):用例描述
  • testcase_name(必填):指定测试类下的测试方法名称
  • config(可选):配置全局变量(配置文件中的全局变量),采用锚点标记的方式,下文可以直接引用
# 示例
# 标记锚点
config: &config
  zone: "self.config.get('zone')"
steps:
  - class_name: 'job'
    method_name: 'submit_job'
    request:
      ...
      data:
        params:
          service: hpc
          ...
          # 引用锚点
          <<: *config # 等同于 zone:"self.config.get('zone')"
  • steps:测试场景所包含的所有接口列表,列表中每一个元素即代表一个接口请求信息。(先后顺序代表接口请求顺序)steps中每个元素中至少有3个字段:

    • class_name(必填):指定ao的类名

    • method_name(必填):指定ao类下的方法名,即接口名称

    • request(必填): 接口请求信息

    • dependent_api(可选):指明该接口的依赖接口,以列表形式接收,可以接收多个依赖,按先后顺序调用,每个依赖需要指明的参数如下:

      • module(必填): 依赖接口的所在模块,从apis模块导入,如apis.cluster

      • api(必填): 依赖接口的方法名称,如list

      • extract(必填): 需要从依赖接口中提取的参数

      • api_params(可选):依赖接口所需要的参数,以字典形式接收

      • cluster_type: ‘ehpc’

# 示例
...
steps:
  - class_name: 'job'
    method_name: 'submit_job'
    dependent_api:
      # 指明依赖apis.cluster模块下的list方法
      - module: apis.cluster
        api: list
        extract: "cluster_id" # 提取的参数名
        api_params: 
          cluster_type: 'ehpc' # 依赖接口所需要外部传入的参数
      # 指明依赖apis.queue模块下的get_queue_list方法
      - module: apis.queue
        api: get_queue_list
        extract: "ehpc_queue_id"
  • dependent_params(可选):指明该接口,需要从依赖接口中引用什么变量,并以什么jsonpath表达式提取。以列表形式接收(注意:当指明dependent_params,就必须同时指明dependent_api),每个依赖参数需要指明的参数如下:
    • params(必填):指明需要提取的依赖参数名
    • jsonpath(必填):指明提取依赖参数的jsonpath表达式
    • index(可选):jsonpath表达式提取出的值是list,可以根据自身需求指明需要提取哪个,默认值为0
# 示例
...
steps:
  - class_name: 'job'
    method_name: 'submit_job'
    dependent_api:
      ...
    dependent_params:
      - params: "hpc_cluster_id" # 依赖的参数名,注意下面引用时,就是用该名称
        jsonpath: "$..cluster_id" # 从依赖接口的响应中根据该表达式提取依赖参数
      - params: "hpcqueue_id" 
        jsonpath: "$..hpcqueue_id"
 request:
      ...
      data:
        params:
          ...
          cluster_id: "$hpc_cluster_id$" # 通过$dependent_params中的params$进行引用
          hpcqueue_id: "$hpcqueue_id$"
  • data_driven(可选):若该接口需要做数据驱动,可以通过该参数指明。 一个业务场景中,若多个接口都有data_driven参数,那么会自动进行参数组合,生成多条这个业务场景的测试数据。 不仅如此,凡是接口中带有data_driven同时还会生成单接口测试数据。
# 示例
...
steps:
  - class_name: 'job'
    method_name: 'submit_job'
    data_driven: # 指明需要数据驱动的参数
      cmd_line: ["sleep 10 ","sleep 15 ","sleep 30 ",....] # 参数名:参数组
    ...
    request:
      ...
      data:
        params:
          ...
          # test_data['cmd_line']的方式标记该参数是需要数据驱动的参数
          cmd_line: "test_data['cmd_line']"
  • assert(可选):若需要对该接口的响应结果进行断言,可添加此参数,以列表形式接收。
# 示例
...
steps:
  - class_name: 'job'
    method_name: 'submit_job'
    ...
    request:
      ...
    assert: 
      - eq: [ 'ret_code',0 ] # [jsonpath表达式(实际结果),jsonpath索引(可不写,默认为0),预期结果]
      - neq: [ '$..hpcjob_uuid',null ] 

ps:支持的断言器可在_aomaker.base.base_testcase_中查看。

YAML完整实例

demo.yaml

testcase_class_name: 'smoke2'
description: 'hpc冒烟'
testcase_name: 'hpc_smoke'
config: &config
  zone: "self.config.get('zone')"
  owner: "self.config.get('owner')"
  user_id: "self.config.get('owner')"
  run_user: "self.config.get('owner')"
steps:
  - class_name: 'cluster'
    method_name: 'list'
    request:
      api_path: /portal_api/
      method: POST
      params:
        action: cluster/list
      data:
        params:
          service: hpc
          action: cluster/list
          status: [ "active" ]
          sort_key: "create_time"
          verbose: 1
          limit: 10
          offset: 0
          reverse: 1
          cluster_type: "ehpc"
          <<: *config
        method: GET
    assert:
      - ge: [ '$..total_count',0 ]

  - class_name: cluster
    method_name: 'list_node'
    dependent_params:
      - params: "hpc_cluster_id"
        jsonpath: "$..cluster_id"
        index: 0
    request:
      api_path: /portal_api/
      method: POST
      params:
        action: cluster/listNodes
      data:
        params:
          action: cluster/listNodes
          service: hpc
          status:
            - active
            - stopped
            - pending
          sort_key: role
          reverse: 1
          verbose: 1
          limit: 10
          offset: 0
          cluster_id: "$hpc_cluster_id$"
          <<: *config
        method: GET
  - class_name: 'user'
    method_name: 'user_list'
    dependent_params:
      - params: "hpc_cluster_id"
        jsonpath: "$..cluster_id"
        index: 0
    request:
      api_path: /portal_api/
      method: POST
      params:
        action: user
      data:
        params:
          service: hpc
          action: user/list
          limit: 10
          offset: 0
          sort_key: 'create_time'
          reverse: 1
          cluster_id: "$hpc_cluster_id$"
          <<: *config
        method: GET
  - class_name: 'user'
    method_name: 'add_user'
    dependent_params:
      - params: "hpc_cluster_id"
        jsonpath: "$..cluster_id"
        index: 0
    data_driven:
      username: [ admin7,admin8,admin9 ]
      password: [ zhu!!jie,zhu@@jie,zhu@10jie ]
    request:
      api_path: /portal_api/
      method: POST
      params:
        action: user/addUser
      data:
        params:
          service: hpc
          action: user/addUser
          mod: '755'
          username: "test_data['username']"
          password: "test_data['password']"
          cluster_id: "$hpc_cluster_id$"
          <<: *config
    assert:
      - eq: [ 'ret_code',0 ]
  - class_name: 'user'
    method_name: 'modify_user'
    dependent_params:
      - params: "hpc_cluster_id"
        jsonpath: "$..cluster_id"
        index: 0
    data_driven:
      username: [ admin7,admin8,admin9 ]
    request:
      api_path: /portal_api/
      method: POST
      params:
        action: user/modifyUser
      data:
        params:
          service: hpc
          action: user/modifyUser
          new_password: Zhu@@@88jie
          username: "test_data['username']"
          cluster_id: "$hpc_cluster_id$"
          <<: *config
    assert:
      - eq: [ 'ret_code',0 ]
  - class_name: 'user'
    method_name: 'delete_user'
    dependent_params:
      - params: "hpc_cluster_id"
        jsonpath: "$..cluster_id"
        index: 0
    data_driven:
      username: [ admin7,admin8,admin9 ]
    request:
      api_path: /portal_api/
      method: POST
      params:
        action: user/deleteUser
      data:
        params:
          service: hpc
          action: user/deleteUser
          username: "test_data['username']"
          cluster_id: "$hpc_cluster_id$"
          <<: *config
    assert:
      - eq: [ 'ret_code',0 ]

  - class_name: 'queue'
    method_name: 'get_queue_list'
    dependent_params:
      - params: "hpc_cluster_id"
        jsonpath: "$..cluster_id"
        index: 0
    request:
      api_path: /portal_api/
      method: POST
      params:
        action: queue/ehpc/getQueueList
      data:
        params:
          service: hpc
          action: queue/ehpc/getQueueList
          limit: 10
          offset: 0
          sort_key: create_time
          reverse: 1
          pageSize: 100
          is_active: 1
          cluster_id: "$hpc_cluster_id$"
          <<: *config
        method: GET
    assert:
      - eq: [ 'ret_code',0 ]

  - class_name: 'job'
    method_name: 'submit_ehpc_job'
    dependent_api:
      - module: apis.cluster
        api: list
        extract: "cluster_id"
        api_params:
          cluster_type: 'ehpc'
      - module: apis.queue
        api: get_queue_list
        extract: "ehpc_queue_id"
    dependent_params:
      - params: "hpc_cluster_id"
        jsonpath: "$..cluster_id"
      - params: "hpcqueue_id"
        jsonpath: "$..hpcqueue_id"
    data_driven:
      cmd_line: [ 'sleep 10','sleep 5', 'sleep 15' ]
    request:
      api_path: /portal_api/
      method: POST
      params:
        action: job/submitJob
      data:
        params:
          service: hpc
          action: job/submitJob
          cmd_line_type: input
          name: 'mitm work'
          priority: 1
          cmd_line: "test_data['cmd_line']"
          cluster_id: "$hpc_cluster_id$"
          hpcqueue_id: "$hpcqueue_id$"
          <<: *config
    assert:
      - eq: [ 'ret_code',0 ]
      - neq: [ 'hpcjob_uuid',null ]


将该demo.yaml文件放在工程根目录下的yamlcase文件夹下,然后命令行进入到工程根目录下,执行命令:

aomaker mcase yamlcase\demo.yaml后,aomaker会直接将yaml文件中的描述,转化为对应代码文件。

apis目录下生成接口文件:

jRLHNd.png

cluster.py

jRLL9I.md.png

数据文件包括单场景和业务场景:

jRLO3t.png

smoke2.yaml

jRLXgP.png

生成的测试文件,包括单接口和业务场景case:

jRLjjf.png

test_smoke2.py

jRLxu8.md.png

results matching ""

    No results matching ""