如何在命令行中使用 SoapUI test运行ner 在另一个项目中 运行 testStep?
How to run testStep in another project using SoapUI testrunner in command line?
我们有几个 soapui 项目,每个项目都在不同的 Web 服务上发送测试请求。然而,执行测试的 Groovy 脚本在大多数情况下对于每个项目都是相同的。因此,我们决定将通用脚本保存在单独的“虚拟”项目(“TestWSScript-soapui-project.xml”)和一个 testsuite/case(Autotest/Test) 只有一个 testStep(Groovy 脚本名为“运行”)。
这个想法是为每个 WebService(比如 WS1-soapui-project.xml)创建一个项目,其中包含一个 TestCase 的 testSuite。在这个测试用例中将是
- Groovy 设置 WS 特定属性并从 TestWSScript-soapui-project.xml
调用通用脚本的测试步骤
- 请求测试步骤以调用网络服务并执行断言
- 结束 Groovy 测试步骤。
这在 SoapUI 中有效,但我想 运行 从 Windows 命令行(用于自动化目的的批处理文件)进行测试。在这里我 运行 遇到了一个问题:当使用
从命令行调用 test运行ner 时
set "SOAPUI_FOR_TEST_DIR=..\..\..\programs\SoapUI-5.6.0"
"%SOAPUI_FOR_TEST_DIR%\bin\testrunner.bat" -sAutoTest -r -a -j -I "..\resources\WS1-soapui-project.xml"
它不会加载包含所有 SoapUi 项目的整个工作区。因此,以下脚本(在 WS1-soapui-project.xml/AutoTest suite/Test TestCase 中)应该 运行 来自项目 TestWSScript-soapui-project.xml/AutoTest suite/Test 的 testStep TestCase returns Null(更具体地说“无法在空对象上调用方法 getProjectByName()”)
import com.eviware.soapui.model.project.ProjectFactoryRegistry
import com.eviware.soapui.impl.wsdl.WsdlProjectFactory
def workspace = testRunner.testCase.testSuite.project.workspace
def testProject = (workspace==null) ?
ProjectFactoryRegistry.getProjectFactory(WsdlProjectFactory.WSDL_TYPE).createNew("TestWSScript.xml") :
workspace.getProjectByName("TestWSScript")
if(!testProject.open && workspace!=null) workspace.openProject(testProject)
// Connect to the test step in another project.
def prj = testRunner.testCase.testSuite.project.workspace.getProjectByName('TestWSScript')
tCase = prj.testSuites['AutoTest'].testCases['Test']
tStep = tCase.getTestStepByName("Run")
// Call the test runner and check if it can run the specified step.
def runner = tStep.run(testRunner, context)
被调用的脚本只是循环遍历从csv文件中读取的参数并调用请求步骤。这与我需要解决的问题无关,因为问题发生在调用脚本之前。
有没有办法实现我们想要的?我们使用的是免费版 SoapUI-5.6.0。
提前致谢。
这是我对你的情况的建议。
- 不要只为一个 groovy 脚本创建一个单独的项目,而是从中创建一个库。
- 根据需要创建 class 和方法。如果您从调用方传递数据,请使用方法参数或 class 成员。
- 使用您现有的脚本,将它们转换成class方法。
- 根据需要创建 class(es)。
- 它可以编程为
java
或 groovy
- 编译 class(es) 并创建库
- 将此库复制到
%SOAPUI_HOME%\bin\ext
目录下
- 现在您可以在任何项目中调用这些方法。不再需要虚拟项目。
- 好在所有的项目都是独立的。
这是 blog 由 SoapUI 输出和作者之一 Rupert Anderson 创建的内容。
Steps
1.Create the following directory structure
soapuilib/src/main/groovy/custom
2.Get some Groovy code
For this example, I have knocked together a simple script to generate sequential ids. It may be of little practical use, but I wanted something with a simple public static method to call. Since the method is static, there will be no need to instantiate the class before calling it in step #8.
[groovy title=”SequentialIdGenerator.groovy”]
package custom
import java.util.concurrent.atomic.AtomicLong
public class SequentialIdGenerator {
public static final long counterSeed = 1000
public static final String prefix = "id"
private static AtomicLong counter = new AtomicLong(counterSeed)
public static String nextId() {
return prefix + counter.incrementAndGet()
}
}
[/groovy]
create the above script as a text file called SequentialIdGenerator.groovy
copy it to soapuilib/src/main/groovy/custom
3.Create Gradle build script
For this part, there are plenty of options to build the code and package it, such as Maven, Ant or just running the right shell commands! The following minimal Gradle script allows us to compile and package the code as a jar in one easy statement.
[code language=”groovy”]
apply plugin: ‘groovy’
version = ‘1.0’
jar {
classifier = ‘library’
manifest {
attributes ‘Implementation-Title’: ‘SoapUI Sample Groovy Library’, ‘Implementation-Version’: version
}
}
repositories {
mavenCentral()
}
dependencies {
compile ‘org.codehaus.groovy:groovy:2.1.7’ //Matches Groovy in SoapUI 5.2.1
}
[/code]
Create the above Gradle script as soapuilib/build.gradle
INFO: Groovy Version – (At time of writing) The current version of Groovy is v2.4.6, but SoapUI 5.2.1 ships with Groovy 2.1.7. If you try to compile with a Groovy version 2.3+ and use it with SoapUI, you will see an error popup and log message in like ‘org/codehaus/groovy/runtime/typehandling/ShortTypeHandling‘ – see http://glaforge.appspot.com/article/groovy-2-3-5-out-with-upward-compatibility for more details and options. Basically, you can still use the latest Groovy version, but will need to include an additional groovy-backports-compat23 dependency!
5.Compile it & Create jar file
Now we’re ready to use the Gradle script to compile the sample script from step #2 and package it as a jar file.
Open a shell/command prompt at soapuilib/
gradle clean build jar
You should then see output like:
[bash]
tests-MacBook-Pro:soapuilib test$ gradle clean build jar
:clean
:compileJava UP-TO-DATE
:compileGroovy
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:compileTestGroovy UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 5.499 secs
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.12/userguide/gradle_daemon.html
[/bash]
and our new library jar file created under the directory:
soapuilib/build/soapuilib-1.0-sample.jar
6.Add jar file to SoapUI
To make our new Groovy library jar available for use in SoapUI, it should be added in SoapUI Home under the following external library directory:
SoapUI ext Directory
Or the Windows equivalent e.g. C:\Program Files\SmartBear\SoapUI-5.2.1\bin\ext
7.Verify jar file is imported
When SoapUI is restarted, you should see the following log entry indicating that the jar file has been successfully added to the SoapUI classpath:
SoapUI ext Lib Loaded
8.Call the code
Our SequentialIdGenerator has a public static method nextId() that we can call, so to do this we can either import the class (Example 1) or just prefix the class with its package (Example 2). See below:
Example 1 – Call from Groovy TestStep:
[code]
import custom.*
log.info SequentialIdGenerator.nextId()
[/code]
Gives output like:
[code]
Thu May 12 16:49:20 BST 2016:INFO:id1001
[/code]
Example 2 – Call from Property Expansion:
[code]
${= custom.SequentialIdGenerator.nextId()}
[/code]
编辑:
这是具有 context, log
变量访问的示例代码。
<script src="https://gist.github.com/nmrao/c489a485bbe3418cf49d8442f9fb92eb.js"></script>
我们有几个 soapui 项目,每个项目都在不同的 Web 服务上发送测试请求。然而,执行测试的 Groovy 脚本在大多数情况下对于每个项目都是相同的。因此,我们决定将通用脚本保存在单独的“虚拟”项目(“TestWSScript-soapui-project.xml”)和一个 testsuite/case(Autotest/Test) 只有一个 testStep(Groovy 脚本名为“运行”)。 这个想法是为每个 WebService(比如 WS1-soapui-project.xml)创建一个项目,其中包含一个 TestCase 的 testSuite。在这个测试用例中将是
- Groovy 设置 WS 特定属性并从 TestWSScript-soapui-project.xml 调用通用脚本的测试步骤
- 请求测试步骤以调用网络服务并执行断言
- 结束 Groovy 测试步骤。
这在 SoapUI 中有效,但我想 运行 从 Windows 命令行(用于自动化目的的批处理文件)进行测试。在这里我 运行 遇到了一个问题:当使用
从命令行调用 test运行ner 时set "SOAPUI_FOR_TEST_DIR=..\..\..\programs\SoapUI-5.6.0"
"%SOAPUI_FOR_TEST_DIR%\bin\testrunner.bat" -sAutoTest -r -a -j -I "..\resources\WS1-soapui-project.xml"
它不会加载包含所有 SoapUi 项目的整个工作区。因此,以下脚本(在 WS1-soapui-project.xml/AutoTest suite/Test TestCase 中)应该 运行 来自项目 TestWSScript-soapui-project.xml/AutoTest suite/Test 的 testStep TestCase returns Null(更具体地说“无法在空对象上调用方法 getProjectByName()”)
import com.eviware.soapui.model.project.ProjectFactoryRegistry
import com.eviware.soapui.impl.wsdl.WsdlProjectFactory
def workspace = testRunner.testCase.testSuite.project.workspace
def testProject = (workspace==null) ?
ProjectFactoryRegistry.getProjectFactory(WsdlProjectFactory.WSDL_TYPE).createNew("TestWSScript.xml") :
workspace.getProjectByName("TestWSScript")
if(!testProject.open && workspace!=null) workspace.openProject(testProject)
// Connect to the test step in another project.
def prj = testRunner.testCase.testSuite.project.workspace.getProjectByName('TestWSScript')
tCase = prj.testSuites['AutoTest'].testCases['Test']
tStep = tCase.getTestStepByName("Run")
// Call the test runner and check if it can run the specified step.
def runner = tStep.run(testRunner, context)
被调用的脚本只是循环遍历从csv文件中读取的参数并调用请求步骤。这与我需要解决的问题无关,因为问题发生在调用脚本之前。
有没有办法实现我们想要的?我们使用的是免费版 SoapUI-5.6.0。 提前致谢。
这是我对你的情况的建议。
- 不要只为一个 groovy 脚本创建一个单独的项目,而是从中创建一个库。
- 根据需要创建 class 和方法。如果您从调用方传递数据,请使用方法参数或 class 成员。
- 使用您现有的脚本,将它们转换成class方法。
- 根据需要创建 class(es)。
- 它可以编程为
java
或groovy
- 编译 class(es) 并创建库
- 将此库复制到
%SOAPUI_HOME%\bin\ext
目录下 - 现在您可以在任何项目中调用这些方法。不再需要虚拟项目。
- 好在所有的项目都是独立的。
这是 blog 由 SoapUI 输出和作者之一 Rupert Anderson 创建的内容。
Steps
1.Create the following directory structure
soapuilib/src/main/groovy/custom
2.Get some Groovy code
For this example, I have knocked together a simple script to generate sequential ids. It may be of little practical use, but I wanted something with a simple public static method to call. Since the method is static, there will be no need to instantiate the class before calling it in step #8.
[groovy title=”SequentialIdGenerator.groovy”]
package custom
import java.util.concurrent.atomic.AtomicLong
public class SequentialIdGenerator {
public static final long counterSeed = 1000
public static final String prefix = "id"
private static AtomicLong counter = new AtomicLong(counterSeed)
public static String nextId() {
return prefix + counter.incrementAndGet()
}
}
[/groovy]
create the above script as a text file called SequentialIdGenerator.groovy
copy it to soapuilib/src/main/groovy/custom
3.Create Gradle build script
For this part, there are plenty of options to build the code and package it, such as Maven, Ant or just running the right shell commands! The following minimal Gradle script allows us to compile and package the code as a jar in one easy statement.
[code language=”groovy”]
apply plugin: ‘groovy’
version = ‘1.0’
jar {
classifier = ‘library’
manifest {
attributes ‘Implementation-Title’: ‘SoapUI Sample Groovy Library’, ‘Implementation-Version’: version
}
}
repositories {
mavenCentral()
}
dependencies {
compile ‘org.codehaus.groovy:groovy:2.1.7’ //Matches Groovy in SoapUI 5.2.1
}
[/code]
Create the above Gradle script as soapuilib/build.gradle
INFO: Groovy Version – (At time of writing) The current version of Groovy is v2.4.6, but SoapUI 5.2.1 ships with Groovy 2.1.7. If you try to compile with a Groovy version 2.3+ and use it with SoapUI, you will see an error popup and log message in like ‘org/codehaus/groovy/runtime/typehandling/ShortTypeHandling‘ – see http://glaforge.appspot.com/article/groovy-2-3-5-out-with-upward-compatibility for more details and options. Basically, you can still use the latest Groovy version, but will need to include an additional groovy-backports-compat23 dependency!
5.Compile it & Create jar file
Now we’re ready to use the Gradle script to compile the sample script from step #2 and package it as a jar file.
Open a shell/command prompt at soapuilib/
gradle clean build jar
You should then see output like:
[bash]
tests-MacBook-Pro:soapuilib test$ gradle clean build jar
:clean
:compileJava UP-TO-DATE
:compileGroovy
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:compileTestGroovy UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 5.499 secs
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.12/userguide/gradle_daemon.html
[/bash]
and our new library jar file created under the directory:
soapuilib/build/soapuilib-1.0-sample.jar
6.Add jar file to SoapUI
To make our new Groovy library jar available for use in SoapUI, it should be added in SoapUI Home under the following external library directory:
SoapUI ext Directory
Or the Windows equivalent e.g. C:\Program Files\SmartBear\SoapUI-5.2.1\bin\ext
7.Verify jar file is imported
When SoapUI is restarted, you should see the following log entry indicating that the jar file has been successfully added to the SoapUI classpath:
SoapUI ext Lib Loaded
8.Call the code
Our SequentialIdGenerator has a public static method nextId() that we can call, so to do this we can either import the class (Example 1) or just prefix the class with its package (Example 2). See below:
Example 1 – Call from Groovy TestStep:
[code]
import custom.*
log.info SequentialIdGenerator.nextId()
[/code]
Gives output like:
[code]
Thu May 12 16:49:20 BST 2016:INFO:id1001
[/code]
Example 2 – Call from Property Expansion:
[code]
${= custom.SequentialIdGenerator.nextId()}
[/code]
编辑:
这是具有 context, log
变量访问的示例代码。
<script src="https://gist.github.com/nmrao/c489a485bbe3418cf49d8442f9fb92eb.js"></script>