我正在开发一个SoapUI项目,我需要使用test runner运行我的测试套件。我正在使用外部groovy脚本编写环境变量。我在这里面临的问题是每当我从测试运行器运行测试用例时,它返回Workspace为null,这在外部groovy中使用。所以在外部groovy我得到工作空间为null导致错误[getProjectByname()不能在null上调用]。
下面是使用工作空间的全局脚本的构造函数
AvengerAPITestManager(String TestProject, String TestSuite, String TestCase,String TestStep)
{
TestName = "AvengerAPITests";
testProject = SoapUI.getWorkspace().getProjectByName(TestProject);
tSuite = testProject.getTestSuiteByName(TestSuite);
tCase = testProject.getTestSuiteByName(TestSuite).getTestCaseByName(TestCase);
tStepName = TestStep.toString();
tStep=testProject.getTestSuiteByName(TestSuite).getTestCaseByName(TestCase).getTestStepByName (TestStep);
}
上面我们有用户SoapUI.getWorkspace()在尝试从soapUI运行时工作正常,但是当我试图从testrunner运行时SoapUI.getWorkspace出来是null。我甚至尝试过传递工作区,就像我传递testproject名称一样,它仍然没有用。
我也试过类似的东西
AvengerAPITestManager(Object workspace,String TestProject, String TestSuite, String TestCase, String TestStep)
{
TestName = "AvengerAPITests";
testProject = workspace.getProjectByName(TestProject);
tSuite = testProject.getTestSuiteByName(TestSuite);
tCase = testProject.getTestSuiteByName(TestSuite).getTestCaseByName(TestCase);
tStepName = TestStep.toString();
tStep = testProject.getTestSuiteByName(TestSuite).getTestCaseByName(TestCase).getTestStepByName(TestStep);
}
在上面的代码中,我尝试从测试用例中传递Workspace对象,因为我传递了Testcase名称,但我仍然为工作区获取null。请告诉我如何处理这个问题。
答案 0 :(得分:1)
这是有用的工作示例https://github.com/stokito/soapui-junit
您应该将sample-soapui-project.xml
放到/src/test/resources
文件夹中,将其暴露给类路径
答案 1 :(得分:0)
如果要在外部代码中使用soap ui,请尝试直接创建具有特定项目文件的新测试运行器:
SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
runner.setProjectFile( "src/dist/sample-soapui-project.xml" );
runner.run();
或者如果您想更精确地定义测试执行,可以使用以下内容:
WsdlProject project = new WsdlProject( "src/dist/sample-soapui-project.xml" );
TestSuite testSuite = project.getTestSuiteByName( "Test Suite" );
TestCase testCase = testSuite.getTestCaseByName( "Test Conversions" );
// create empty properties and run synchronously
TestRunner runner = testCase.run( new PropertiesMap(), false );
PS:不要忘记导入你在代码中使用的soap ui类,并将它们放到classpath中。
PPS:如果你只需要在soap ui之外运行测试用例和/或自动化这个过程,为什么不只是使用testrunner.sh/.bat做同样的事情? (这里是这种描述:http://www.soapui.org/Test-Automation/functional-tests.html)
答案 2 :(得分:0)
我不确定这是否会对那里的任何人有所帮助,但这里是我所做的,以解决我的工作空间因为null导致错误的问题[getProjectByname()无法在null上调用]当我从cmd运行时 试试这个:
import com.eviware.soapui.model.project.ProjectFactoryRegistry
import com.eviware.soapui.impl.wsdl.WsdlProjectFactory
import com.eviware.soapui.impl.wsdl.WsdlProject
//get the Util project
def project = null
def workspace = testRunner.testCase.testSuite.project.getWorkspace();
//if running Soapui
if (workspace != null) {
project = workspace.getProjectByName("Your Project")
}
//if running in Jenkins/Hudson
else{
project = new WsdlProject("C:\\...\\....\\....\\-soapui-project.xml");
}
if (project.open && project.name == "Your Project") {
def properties = new com.eviware.soapui.support.types.StringToObjectMap()
def testCase = project.getTestSuiteByName("TestSuite 1").getTestCaseByName("TestCase");
if(testCase == null)
{
throw new RuntimeException("Could not locate testcase 'TestCase'! ");
} else {
// This will run everything in the selected project
runner = testCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false)
}
}
else {
throw new RuntimeException("Could not find project ' Order Id....' !")
}
以上代码将运行所选项目中的所有内容。