
# 创建用例 - CreateNewCase
#### 功能介绍
创建用例
#### 调试
您可以在[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/CPTS/doc?api=CreateNewCase)中调试该接口，支持自动认证鉴权。API Explorer可以自动生成SDK代码示例，并提供SDK代码示例调试功能。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-cpts/cpts_api_0019.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                                          | 访问级别  | 资源类型（\*为必须） | 条件键                       | 别名 | 依赖的授权项 |
  |:---|:---|:---|:---|:---|:---|
  | codeartsperftest:cpts:createPerfTestResource | Write | cpts \*     | g:ResourceTag/\<tag-key\> | -  | -      |
     
  
 
#### URI
POST /v2/{project_id}/test-cases
表1路径参数 
| 参数         | 是否必选 | 参数类型   | 描述                                                                              |
|:---|:---|:---|:---|
| project_id | 是    | String | 表示项目ID，参见[获取方法说明](https://support.huaweicloud.com/api-cpts/cpts_api_0017.html)。 |
   
#### 请求参数
表2请求Body参数 
| 参数                 | 是否必选 | 参数类型                                                                    | 描述                                           |
|:---|:---|:---|:---|
| name               | 是    | String                                                                  | 用例名称                                         |
| case_type          | 否    | Integer                                                                 | 用例类型（0：常规用例）                                 |
| contents           | 是    | Array of [Contents] objects           | 用例脚本信息                                       |
| for_loop_params    | 否    | Array of objects                                                        | 旧版本逻辑控制器字段，当前已不使用                            |
| increase_setting   | 否    | Array of objects                                                        | 梯度递增                                         |
| stages             | 是    | Array of [TestCaseStage] objects | 阶段信息                                         |
| status             | 否    | Integer                                                                 | 状态（0：已删除；1：启用；2：禁用）                          |
| temp_id            | 否    | Integer                                                                 | 用例ID，通过"创建用例"接口获取响应体中的json.test_case_id字段的值。 |
| sort               | 否    | Integer                                                                 | 排序字段                                         |
| directory_id       | 是    | Integer                                                                 | 用例所属目录ID（旧版接口可不传）                            |
| setup_contents     | 否    | Array of [Contents] objects           | 前置步骤                                         |
| user_replicas      | 否    | Integer                                                                 | 执行器个数                                        |
| collect_log_policy | 否    | Integer                                                                 | 日志采集策略（0-请求模式；1-用例模式）                        |
| link_app_list      | 否    | Array of integers                                                       | 关联全链路应用列表                                    |
| case_info          | 否    | [CaseDoc] object                       | 文本用例信息                                       |
   
 表3TestCaseStage 
| 参数                      | 是否必选 | 参数类型                                                          | 描述                                                 |
|:---|:---|:---|:---|
| operate_mode            | 否    | Integer                                                       | 压力阶段模式（0：时长模式；1：次数模式）                              |
| name                    | 是    | String                                                        | 阶段名称                                               |
| time                    | 是    | Integer                                                       | 压测时长（单位：秒）                                         |
| start_time              | 否    | Integer                                                       | 开始时间                                               |
| end_time                | 否    | Integer                                                       | 结束时间                                               |
| issue_num               | 是    | Integer                                                       | 最大并发数                                              |
| count                   | 否    | Integer                                                       | 次数模式发送总次数                                          |
| pressure_mode           | 否    | Integer                                                       | 压力模式（0：并发模式；1：TPS模式；2：摸高模式；3：浪涌模式；5：震荡模式；7：智能摸高模式） |
| tps_value               | 否    | Integer                                                       | TPS模式下TPS值                                         |
| current_user_num        | 是    | Integer                                                       | 起始并发数                                              |
| current_tps             | 否    | Integer                                                       | 起始TPS值                                             |
| voltage_regulating_mode | 否    | Integer                                                       | 调压模式（0：自动调压模式；1：手动调压模式）                            |
| maximum                 | 否    | Integer                                                       | 震荡/浪涌模式下最大并发数                                      |
| minimum                 | 否    | Integer                                                       | 震荡/浪涌模式下最小并发数                                      |
| loop_count              | 否    | Integer                                                       | 震荡/浪涌次数                                            |
| max_duration            | 否    | Integer                                                       | 浪涌模式下峰值持续时间                                        |
| ramp_up                 | 否    | Integer                                                       | 摸高模式下爬坡时长（单位：秒）                                    |
| peak_load_kpis          | 否    | [StageKpiItems] object | 摸高指标                                               |
| step_duration           | 否    | Integer                                                       | 智能摸高模式下单步执行时长                                      |
| step_size               | 否    | Integer                                                       | 智能摸高模式下递增并发数                                       |
   
 表4StageKpiItems 
| 参数                    | 是否必选 | 参数类型                                                        | 描述     |
|:---|:---|:---|:---|
| average_response_time | 否    | [StageKpiItem] object | 平均响应时间 |
| success_rate          | 否    | [StageKpiItem] object | 成功率    |
   
 表5StageKpiItem 
| 参数         | 是否必选 | 参数类型    | 描述  |
|:---|:---|:---|:---|
| comparison | 否    | String  | 比较符 |
| value      | 否    | Integer | 比较值 |
   
 表6Contents 
| 参数                 | 是否必选 | 参数类型                                                        | 描述                                                                                 |
|:---|:---|:---|:---|
| content_id         | 否    | Integer                                                     | 事务ID，通过"创建事务"接口获取响应体中的tempId字段的值。若不为0表示此卡片为事务；为0表示非事务                              |
| content            | 否    | Array of [Content] objects | 内容                                                                                 |
| index              | 否    | Integer                                                     | 排序索引标识                                                                             |
| selected_temp_name | 否    | String                                                      | 选择的事务或者用例名称                                                                        |
| data               | 否    | Object                                                      | 数据（循环、条件控制器作用的数据）                                                                  |
| data_type          | 否    | Integer                                                     | 数据指令类型（0：默认请求卡片；1：数据指令；201：循环指令；202：条件指令；301：集合点；203：vu百分比控制器；204：吞吐量控制器；302：插件请求） |
| conditions         | 否    | Object                                                      | 若类型为"202：条件指令"，该字段为条件配置                                                            |
| is_disabled        | 是    | Boolean                                                     | 是否禁用                                                                               |
   
 表7Content 
| 参数           | 是否必选 | 参数类型                                                      | 描述                               |
|:---|:---|:---|:---|
| content_type | 是    | Integer                                                   | 用例卡片类型（1：思考时间；2：报文；3：检查点；4：变量提取） |
| content      | 是    | [ContentInfo] object | 内容                               |
   
 表8ContentInfo 
| 参数                   | 是否必选 | 参数类型                                                                    | 描述                                                                          |
|:---|:---|:---|:---|
| body_type            | 是    | Integer                                                                 | body类型（0：字符串；1：form-data格式；3：x-www-form-urlencoded格式）                       |
| bodys                | 否    | Array of objects                                                        | 请求体                                                                         |
| check_end_length     | 否    | Object                                                                  | TCP/UDP协议返回数据长度                                                             |
| check_end_str        | 否    | Object                                                                  | TCP/UDP协议返回结束符                                                              |
| check_end_type       | 否    | Object                                                                  | TCP/UDP协议返回结束类型（1：返回数据长度；2：结束符）                                             |
| connect_timeout      | 否    | Integer                                                                 | 超时时间                                                                        |
| connect_type         | 否    | Integer                                                                 | 连接设置，当前版本已不使用                                                               |
| headers              | 否    | Array of [ContentHeader] objects | 请求头                                                                         |
| http_version         | 是    | String                                                                  | HTTP版本                                                                      |
| method               | 是    | String                                                                  | HTTP方法                                                                      |
| name                 | 是    | String                                                                  | 用例名称                                                                        |
| protocol_type        | 否    | Integer                                                                 | 协议类型（1：HTTP；2：HTTPS；3：TCP；4：UDP；7：HLS/RTMP；9：WebSocket；10：HTTP-FLV；11：MQTT） |
| return_timeout       | 是    | Integer                                                                 | 响应超时                                                                        |
| return_timeout_param | 否    | String                                                                  | 响应超时参数                                                                      |
| url                  | 是    | String                                                                  | 请求地址                                                                        |
| rtmp_url             | 否    | String                                                                  | rtmp地址                                                                      |
| flv_url              | 否    | String                                                                  | flv地址                                                                       |
| bitrate_type         | 否    | Integer                                                                 | 分辨率策略                                                                       |
| duration             | 否    | Integer                                                                 | 持续时间                                                                        |
| retry_delay          | 否    | Integer                                                                 | HLS重试延迟时间                                                                   |
| retry_time           | 否    | Integer                                                                 | HLS重试次数                                                                     |
   
 表9ContentHeader 
| 参数    | 是否必选 | 参数类型   | 描述 |
|:---|:---|:---|:---|
| key   | 是    | String | 键  |
| value | 是    | String | 值  |
   
 表10CaseDoc 
| 参数          | 是否必选 | 参数类型                                                    | 描述                             |
|:---|:---|:---|:---|
| description | 否    | String                                                  | 用例描述信息                         |
| label_names | 否    | Array of strings                                        | 标签                             |
| preparation | 否    | String                                                  | 前置条件                           |
| rank        | 否    | Integer                                                 | 用例等级（0-L0；1-L1；2-L2；3-L3；4-L4） |
| status_code | 否    | Integer                                                 | 状态（0-新建；5-设计中；6-测试中；7-完成）      |
| steps       | 否    | Array of [steps] objects | 测试步骤                           |
   
 表11steps 
| 参数            | 是否必选 | 参数类型   | 描述   |
|:---|:---|:---|:---|
| expect_result | 否    | String | 步骤描述 |
| test_step     | 否    | String | 预期结果 |
   
#### 响应参数
**状态码：200**
表12响应Body参数 
| 参数      | 参数类型                                         | 描述     |
|:---|:---|:---|
| code    | String                                       | 响应码    |
| json    | [json] object | 响应json |
| message | String                                       | 响应消息   |
   
 表13json 
| 参数           | 参数类型    | 描述                                           |
|:---|:---|:---|
| test_case_id | Integer | 用例ID，通过"创建用例"接口获取响应体中的json.test_case_id字段的值。 |
   
#### 请求示例
创建一个用例，包括用例的请求信息，阶段信息等等。
```
/v2/{project_id}/test-cases
{
  "name" : "new_case",
  "case_type" : 0,
  "setup_contents" : [ ],
  "contents" : [ {
    "content" : [ {
      "content" : {
        "body_type" : 0,
        "bodys" : [ ],
        "check_end_length" : null,
        "check_end_str" : null,
        "check_end_type" : null,
        "connect_timeout" : 5000,
        "connect_type" : 1,
        "headers" : [ {
          "key" : "Content-Type",
          "value" : "application/json"
        } ],
        "http_version" : "HTTP/1.1",
        "method" : "GET",
        "name" : "request_example",
        "protocol_type" : 1,
        "return_timeout" : 5000,
        "return_timeout_param" : null,
        "url" : "http://117.xxx.xxx.xxx",
        "duration" : 120,
        "retry_delay" : 6000,
        "retry_time" : 2
      },
      "content_type" : 2
    } ],
    "content_id" : 0,
    "data" : null,
    "data_type" : 0,
    "index" : 0,
    "conditions" : [ ],
    "is_disabled" : false
  } ],
  "for_loop_params" : [ ],
  "increase_setting" : [ ],
  "stages" : [ {
    "count" : 1,
    "current_user_num" : 1,
    "issue_num" : 1,
    "name" : "Stage-2hih",
    "operate_mode" : 0,
    "pressure_mode" : 0,
    "time" : 60,
    "tps_value" : 1,
    "voltage_regulating_mode" : 0,
    "maximum" : 3,
    "minimum" : 1,
    "loop_count" : 2,
    "max_duration" : 15,
    "step_size" : 1,
    "step_duration" : 30,
    "ramp_up" : 30,
    "peak_load_kpis" : {
      "average_response_time" : {
        "comparison" : "<=",
        "value" : 60000
      },
      "success_rate" : {
        "comparison" : ">=",
        "value" : 0
      }
    }
  } ],
  "user_replicas" : 0,
  "collect_log_policy" : 0,
  "link_app_list" : [ ],
  "case_id" : -1,
  "sort" : 1,
  "status" : 1,
  "directory_id" : 174582,
  "case_info" : {
    "description" : "",
    "label_names" : [ ],
    "preparation" : "",
    "rank" : 1,
    "status_code" : 0,
    "steps" : [ {
      "expect_result" : "",
      "test_step" : ""
    } ]
  },
  "temp_id" : 0
}
```
#### 响应示例
无
#### 状态码
| 状态码 | 描述       |
|:---|:---|
| 200 | 创建用例响应对象 |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-cpts/ErrorCode.html)。
