如何使用线程在 SoapUI 中 运行 两次 soap 请求?

How to run twice soap request in SoapUI using threads?

在 SoapUI 中,我准备了包含三个测试步骤的 TestCase(选项 - 生成数据):

  1. 属性
  2. Groovy 脚本
  3. 生成(禁用)

在此测试中,我只想运行第三步同时两次(这就是我不使用 LoadTest 的原因)并验证获得的结果 - 它们应该是不同的。为此,我编写了一个简单的脚本,如下所示。

def testData = testRunner.testCase.getTestStepByName("Properties");

class MyThread extends Thread {
def i;
def testData;

MyThread(def i, def testData) {
    this.i = i;
    this.testData = testData;
}

 void run() {
    generateData();     
}

void generateData() {
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);

    def testCaseStep = testRunner.testCase.testSuite.testCases["Options - generate data"];
    def soapStep = testCaseStep.testSteps["Generate"];

    soapStep.run(testRunner, context);
    def xmlResponse = groovyUtils.getXmlHolder("Generate#response")
    def responseData = xmlResponse.getNodeValues("//ns2:getResponse/ns2:response");

    def row = "row" + this.i;

    this.testData.setPropertyValue(row, responseData.toString());
}
}

MyThread thread1 = new MyThread(0, testData);
MyThread thread2 = new MyThread(1, testData);

thread1.start();
thread2.start();

while ((thread1.isAlive() &&  thread2.isAlive())) {
    def data1 = testData.getPropertyValue("row0");
    def data2 = testData.getPropertyValue("row1");
    assert data1 != data2;
    }

不幸的是,此代码无法正常工作 - 我没有任何错误,但 SOAP 请求未启动,未创建新变量且断言失败。

你能告诉我要取得这个测试的好成绩需要什么吗?

如有任何帮助,我将不胜感激。

代码中修复的一些内容:

  • context用于方法,不可用
  • 待加入的线程
  • 获取数据时需要对象引用

固定代码:

def testData = context.testCase.getTestStepByName("Properties")

class MyThread extends Thread {
    def threadId
    def testData
    def context

    MyThread(def i, def testData, def context) {
            threadId = i
            this.testData = testData
            this.context = context
    }

    void run() {
            generateData()
    }

    void generateData() {
        def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
        def soapStep = context.testCase.testSteps["Generate"]
        soapStep.run(context.testRunner, context)
        def xmlResponse = groovyUtils.getXmlHolder("Generate#response")
        def responseData = xmlResponse.getNodeValues("//ns2:getResponse/ns2:response")    
        testData.setPropertyValue("row${threadId}" as String, responseData.toString())
    }
}

MyThread thread1 = new MyThread(0, testData, context)
MyThread thread2 = new MyThread(1, testData, context)

thread1.start()
thread1.join()
thread2.start()
thread2.join()

while ((thread1.isAlive() &&  thread2.isAlive())) {
    def data1 = thread1.testData.getPropertyValue(thread1.threadId)
    def data2 = thread2.testData.getPropertyValue(thread2.threadId)
    assert data1 != data2
}