测试定义语法

该测试定义为一个包含以下部分的 YAML 文件:

 requirements: {}

info: 
  title: "New Configuration YAML File"
  version: 2

configs:
  globalVariables:
    protocol: https://
    domain: sample-api.us-east-a.apiconnect.automation.ibm.com
    basePath: /bank
  
  inputs:
    - default:
        value: string
        type: array
    - second:
        value: number
        type: string

  steps:
    - type: request
      method: get
      url: "{{ configs.globalVariables.protocol }}{{ domain }}{{ globalVariables.basePath }}/branches"
      var: branches
      mode: json
    
    - type: assert-is
      expression: branches
      value: "{{ type }}"
    
    - type: assert-exists 
      expression: branches.[1]
      stoponfail: true
    
    - type: assert-is 
      expression: branches.[0].type
      value: string
      stoponfail: true    
    
    - type: assert-is 
      expression: branches.[0].address.city
      value: string
      stoponfail: true  
    
    - type: assert-compares 
      expression1: branches.[0].type
      expression2: branches.[7].type  
    
    - type: request
      method: get
      url: https://sample-api.us-east-a.apiconnect.automation.ibm.com/orders/order/AB1234
      var: payload
      mode: json
    
    - type: assert-equals 
      expression: payload_response_statusCode
      value: 200
      ifexists: false
      stoponfail: false
    
    - type: assert-equals 
      expression: payload_response_header_Content-Type
      value: application/json
    
    - type: assert-in 
      expression: payload_response_header_Content-Type
      values: 
        - application/json
        - application/swagger+yaml
    
    - type: assert-contains 
      expression: payload_response_header_Content-Type
      value: app
    
    - type: assert-matches 
      expression: payload_response_header_Content-Type
      value: ^[a-z]+/json
    
    - type: assert-exists 
      expression: "payload"  
      stoponfail: false
    
    - type: assert-is 
      expression: "payload.tracking_reference"
      value: "string"
      ifexists: "false" 
      stoponfail: false
    
    - type: assert-exists 
      expression: payload.shipped_at
      stoponfail: false
    
    - type: assert-exists 
      expression: payload.status
      stoponfail: false
    
    - type: assert-exists 
      expression: payload.created_at  
      stoponfail: false

表 1. type 属性支持的参数摘要

表 1. 参数、描述和子属性
参数 描述 子属性
globalVariables 全局变量是指在整个测试过程中保持一致的设置,通常在不同测试运行之间不会发生变化。 您可以在此根据需求定义任何属性。 例如: - portocol:指定所有请求中使用的协议(例如 https://),以确保通信安全。 - domain:指定 API 请求的主服务器地址。 - basepath:定义所有 API 端点的基路径(例如 ) /bank,该路径将附加到域名后用于构建请求 URL。
输入 定义了在多个测试场景中使用的各种输入参数集。 您可以创建多个数据集,并为其添加特定测试场景所需的任何属性。 例如,default 和 second 是两个数据集,每个数据集都有针对不同场景量身定制的属性。 注意:必须至少有一个数据集才能运行该测试。
请求 发出 http 请求。 - method:定义要使用的 HTTP 方法。 - url:定义要发送请求的 URL。 - params:定义要发送的参数的键值对。 - headers:定义要发送的头信息的键值对。 - var:定义用于存储响应的变量名称。 - mode:定义预期接收的数据类型以及解析方式(支持:json)。 例如:
- type: request
- method: get
- url: https://sample-api.us-east-a.apiconnect.automation.ibm.com/bank/branches
- var: branches
- mode: json
assert-is 断言响应中的变量类型为 value。 - 表达式:定义待测试的变量。 - 值:定义预期返回的类型。 例如:
- type: assert-is
expression: branches
value: array
assert-exists 断言表达式中指定的某个变量存在。 - expression:定义待测试的变量。 - stoponfail:如果其中一项测试失败,stoponfail 属性将停止执行剩余的测试。 为此,必须将 stoponfail 设置为 true。 例如:
- type: assert-exists
expression: branches.[1]
stoponfail: true
assert-equals 断言变量等于该值。 - 表达式:定义待测试的变量。- 值:定义用于比较的值。 例如:
- type: assert-equals
expression: payload_response_header_Content-Type
value: application/json
assert-greater 断言该变量大于该值。 - 表达式:定义待测试的变量。- 值:定义用于比较的值。 例如:
- type: assert-greater
expression: payload_temperature
value: application/json
无断言 断言该变量小于该值。 - 表达式:定义待测试的变量。- 值:定义用于比较的值。 例如:
- type: assert-less
expression: payload_temperature
value: application/json
-
assert-in 断言由给定表达式标识的元素与给定列表中的至少一个项目匹配。 - 表达式:定义待测试的变量。 - 值:定义一个用于比较的值数组 against.For 示例:
- type: assert-in
expression: payload_response_status
value: application/json
assert-matches 断言由给定表达式标识的元素的值符合指定格式。 - 表达式:定义待测试的变量。 - 值:定义用于比较的正则表达式值 against.For 示例:
- type: assert-matches
expression: payload_response_header_Content-Type
value: [a-z]+/json
assert-compares 断言两个元素在某种程度上是等价的。 - expression1: 指定用于比较的第一个元素的路径。- expression2: 指定用于比较的第二个元素的路径。 例如:
- type: assert-compares
expression1: branches.[0].type
expression2: branches.[7].type
assert-contains 断言由给定表达式标识的元素的值包含一个特定的子字符串。 - 表达式:定义待测试的变量。- 值:定义用于比较的子字符串。 例如:
- type: assert-contains
expression: payload_response_header_Content-Type
value: app
每个 遍历表达式中引用的数组,并对找到的每个元素执行子步骤。 - 表达式:将变量定义为 test.For。示例:
- type: each
expression: "analytics.events"
如果 如果满足指定的条件(在表达式中定义),则“if-condition”步骤中的断言将被执行;否则,这些断言将被跳过。 - 表达式:待测试的条件 - 步骤:如果条件成立,ATM 将执行的子步骤。例如, value: "string" expression: "payload.tracking_reference" - type: assert-is steps: expression: "{{payload_response_statusCode}} == 200"- type: if stoponfail: false