首页 > 解决方案 > 我如何将一个参数发送到rest api,它可以为空或有一些值?

问题描述

我如何将一个参数发送到rest api,它可以为空或有一些值?我想用json发送这个参数。数据是从外部文件中读取的,我的问题是该文件有几行,在其中一些行中,我的参数值是空的,有些包含数据我想自动删除自动测试中为空的参数,甚至删除键直到没有发生异常。

"length": "${DataSource#Length}",
"offset": "${DataSource#Offset}",
"cityCode": "${DataSource#CityCode}",
"cityName" : "${DataSource#CityName}" ,
"provinceCode": "${DataSource#ProvinceCode}",
"provinceName" : "${DataSource#ProvinceName}"

来自soapui pro的截图

标签: jsonrestautomated-testssoapuinullable

解决方案


您可以在运行 API 之前编写一个groovy 步骤,您将在其中根据变量中的空值准备输入

逻辑:-

  1. 从数据源中提取值
  2. 检查它们是否为空不要将它们添加到输入中
  3. 所以最终准备好的字符串存储在测试用例属性中
  4. 该属性可以在实际的休息请求中扩展

`

String jsonInput=""

def Length=context.expand("${DataSource#Length}")
def offset=context.expand("${DataSource#Offset}")

if(Length)
 {
    jsonInput=jsonInput+Length
 }
if(offset!=null)
{
 jsonInput=jsonInput+offset
 }

testRunner.testCase.setPropertyValue("jsonInput",jsonInput)

现在在 Rest 步骤中,您可以提及

  ${#TestCase#jsonInput}

推荐阅读