You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SoapUI中Groovy脚本跨TestCase测试步骤全属性批量同步咨询

优化后可直接运行的Groovy脚本

import com.eviware.soapui.impl.wsdl.*
import com.eviware.soapui.impl.wsdl.testcase.*
import com.eviware.soapui.model.testsuite.*

// 定位跨项目的目标测试用例M_TC_Add
def traceStep = testRunner.testCase.getTestStepByName("TRACING_TC_Addition")
def projectName = traceStep.getPropertyValue("PROJECT_NAME")
def testSuiteName = traceStep.getPropertyValue("TEST_SUITE_NAME")
def testCaseName = traceStep.getPropertyValue("TEST_CASE_NAME")
def targetTestCase = testRunner.testCase.testSuite.project.workspace
        .getProjectByName(projectName)
        .getTestSuiteByName(testSuiteName)
        .getTestCaseByName(testCaseName)

// 1、同步S_TC_Addition的LIST步骤所有非空属性到目标测试用例的LIST步骤
def sourceListStep = testRunner.testCase.getTestStepByName("LIST")
def targetListStep = targetTestCase.getTestStepByName("LIST")
// 遍历源LIST所有属性
sourceListStep.propertyNames.each { propName ->
    def propValue = sourceListStep.getPropertyValue(propName)
    // 仅同步有值的属性,过滤空值
    if (propValue != null && propValue.trim() != "") {
        // 目标步骤存在该属性才赋值,避免报错
        if (targetListStep.hasProperty(propName)) {
            targetListStep.setPropertyValue(propName, propValue)
        }
    }
}

// 执行目标测试用例
targetTestCase.run(null, false)

// 2、将目标测试用例EXIT步骤的所有属性回写到S_TC_Addition的EXIT步骤
def sourceExitStep = testRunner.testCase.getTestStepByName("EXIT")
def targetExitStep = targetTestCase.getTestStepByName("EXIT")
targetExitStep.propertyNames.each { propName ->
    def propValue = targetExitStep.getPropertyValue(propName)
    if (sourceExitStep.hasProperty(propName)) {
        sourceExitStep.setPropertyValue(propName, propValue)
    }
}

核心逻辑说明

  • 抽离了目标测试用例的获取逻辑,仅查询一次,减少重复代码,提升运行效率
  • 调用propertyNames遍历测试步骤下的所有自定义属性,不需要手动指定属性名,兼容2~100个甚至更多属性的场景
  • 同步LIST步骤时增加了非空判断,只同步带值的属性,符合需求要求
  • 回写EXIT步骤时全量同步所有属性和对应值,不管属性名是什么都可以自动适配
  • 增加了属性存在性判断,避免目标/源步骤缺少对应属性时抛出异常,脚本兼容性更强

方案示意图

方案示意图

内容的提问来源于stack exchange,提问作者Arah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 17:06:04