首页 > 解决方案 > 如何使用 Groovy 并行运行 SoapUI 测试步骤

问题描述

SoapUI 可以选择并行运行您的测试套件和测试用例,但没有使用测试步骤来执行此操作的选项。

如何在我的测试用例中使用 Groovy 测试步骤来实现这样的目标?

这是我当前的代码:

    import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner

//Get all Soap type test steps within the testCases
for ( testStep in testRunner.testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.class)){
    //Print out the name for the testStep
    tsname = testStep.getName();
    log.info tsname;

    //thread = new Thread("$tsname")
    def th = new Thread("$tsname") {
                    @Override
            public void run(){
            //Set the TestRunner to the respective TestCase
            TestRunner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null);
            //Run them all and then rejoin the threads
            log.info("thread: " + tsname)
            TestRunner.runTestStepByName(tsname);
            def soapResponse = TestRunner.getTestCase().getTestStepByName(tsname).getProperty("Response");
            log.info "soapResponse: " + soapResponse;
            th.join();
            }
    }
    th.start();
    th.join();
}

标签: multithreadinggroovysoapui

解决方案


设法自己解决了这个问题,并在这里为所有遇到同样问题的人发布了答案:

import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep

List<String> steps = testRunner.testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.class)
def map = [:]

//define the threads list, this will hold all threads
def threads = []

def kickEm = steps.each{ step ->

    def th = new Thread({

        stepName = step.getName();
        log.info "Thread in start: " + step.getName();
        //Set the TestRunner to the respective TestCase
        TestRunner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null);
        //Run the corresponding teststep
        TestRunner.runTestStepByName(step.getName());
        //Get the response of the current step
        def soapResponse = context.expand(TestRunner.getTestCase().getTestStepByName(step.getName()).getPropertyValue('Response')) as String;
        log.info "In thread "+step.getName()+" soapResponse: " + soapResponse
        //Put this into a map, the key is the stepname and the value is its response, so we can retrieve it all outside the each loop
        map.put(step.getName(), soapResponse);
    })

   log.info "Putting new Thread({..}) th back into the threads list";
   threads << th;
}

threads.each { it.start(); }

threads.each { it.join(); }

log.info "Map: "

map.each { step, response ->
    log.info "Step: ${step} has the response ${response}"
    };

log.info "Done!"

推荐阅读