SoapUI & Groovy - 如何使用 Run testCase 从不同的 TestCase 获取属性



提前抱歉。我相信这不是一个新问题,但我发誓我已经找了好几天,尝试了几种解决方案,但没有一种完全适合我的需要。我希望你们能帮助我……

我问题:

    我有一个主脚本发送bash命令到我们的服务器:
TestSuite: Tools;
   TestCase: sendBashCommands;
      TestStep: groovycript;
  • 这个测试脚本被几个测试用例使用"Run testCase"调用。每个测试用例有一个不同的bash命令:
  • TestSuite: ServerInfo
       TestCase: getServerVersion
          Property: BashCommand="cat smt | grep Version"
          TestStep: Run sendBashCommands
       TestCase: getServerMessage
          Property: BashCommand="cat smt | grep Message"
          TestStep: Run sendBashCommands
       ...
    

    On my sendBashCommands。我已经尝试了以下命令:

    //def bashCmd= context.expand( '${#BashCommand}' );
         //it returns empty;
    //def bashCmd= testRunner.testCase.getPropertyValue( "BashCommand" );
         //it returns null;
    //def bashCmd= context.getTestCase().getPropertyValue( "BashCommand" );
         //it returns null;
    def bashCmd = context.expand('${#TestCase#BashCommand}');
         //it also returns empty;
    

    目前我使用一个解决方案,它与项目属性的工作,但我真正需要的是在调用sendBashCommand脚本的测试用例的级别上使用这些属性。这可能吗?我怎么能做到呢?

    我认为你这样做是为了维护…根据您的方法,结果必须为null或空。因为您的groovyscript不能直接获得其他测试用例的属性。如果直接调用getProperty()方法,那么它将引用当前testStep的属性。所以下面的方法可以帮助你。

    将以下代码放入您的单独测试用例中,"getServerVersion"等

    def currentProject = testRunner.testCase.testSuite.project
    // the following is to store the names of the test suite and the test cases from which you want to call your main script
    currentProject.setPropertyValue("TestSuiteName",testRunner.testCase.getTestSuite().getLabel().toString())
    currentProject.setPropertyValue("TestCaseName", testRunner.getTestCase().getLabel().toString())
    // call sendBashCommands here -> run sendBashCommands
    

    将此代码放入您的主groovy脚本(sendBashCommands.groovyscript)

    def currentProject = testRunner.testCase.testSuite.project
    def callingTestCase = currentProject.testSuites[currentProject.getPropertyValue("TestSuiteName")].testCases[currentProject.getPropertyValue("TestCaseName")]
    def bashCmd = callingTestCase.getPropertyValue( "BashCommand" )
    // to verify
    log.info bashCmd 
    

    项目对于所有的测试套件都是通用的,所以你必须在任何情况下使用项目属性,也就是说,对于你的需求:)享受:)

    最新更新