SoapUI + Groovy 接口自动化
假设测试用例的实现是由 ?: ?调用不相同的接口(service),组合形式不一样可以形成不同的测试场景 比如下列用例的设计: 需要测试的API 设计的用例有以下两个范围: 1.?需要在不同的前提条件下,调用相同的被测API并且参数设置一样命令而返回不同的结果,并且进行结果验证 (该范围重点是前提条件不一致) 2.在相同的前提下,调用相同的被测API并且参数设置不一样,返回结果不同进行结果验证(改范围重点是验证被测API的参数设置) 范围2是我们目前常用的API测试设计范围,在此不以讨论;我们着重在范围1. 假设设置不同测试场景的前提需要调用的接口(service)有: Service1,?Service2,?Service3 被验证的API: API? 如下图: TestSuite包含以下两个case:PublicCase,TestCase 把设置前提条件的接口放在PublicCase里面 把需要验证的API房子TestCase1里面 note: 1. 在PublicCase里面针对需要对不同service的不同操作进行用例编写,如上图例子:调用的操作为create/delete,并且有对应操作后的get操作(由于create和delete后需要对数据进行校验;因此例子中有两个get并且分别加上自己的校验信息) 2.在项目开始时可用提前把PublicCase的步骤包含校验全部写完 note: 这个TestCase的重点在于TestSenarios groovy的脚本编写,由于普通的soapUI case是顺序执行,我们在此利用groovy控制case的执行顺序 一下是Test Scenario里面的脚本编写: /* * This below scenarios are for API test */ //use below to login public data def testproject = testRunner.testCase.testSuite.project def testcase = testproject.testSuites['TestSuite'].testCases['PublicCase'] testRunner.testCase.setPropertyValue( "count","0") def str_index = testRunner.testCase.getPropertyValue("count") index = Integer.parseInt(str_index) testRunner.runTestStepByName("Login") testRunner.runTestStepByName("Property Transfer") while( index < 4 ){ log.info("this is the case: " + index + " running............") switch (index ){ case (0) : //call the tested scenario1 testRunner.runTestStepByName("CreateAPI1-Scenario1") break case (1): //call public data testcase.testSteps["CreateService1"].run(testRunner,context) testcase.testSteps["GetService1"].run(testRunner,context) //call the tested scenario2 testRunner.runTestStepByName("CreateAPI1-Scenario2") break case (2): //call public data testcase.testSteps["GetService2"].run(testRunner,context) testcase.testSteps["GetService2"].run(testRunner,context) //call the tested scenario3 testRunner.runTestStepByName("CreateAPI1-Scenario3") break case (3): //call public data testcase.testSteps["GetService3"].run(testRunner,context) testcase.testSteps["GetService3"].run(testRunner,context) //call the tested scenario4 testRunner.runTestStepByName("CreateAPI1-Scenario4") break default: println "now no case running............" } index = index +1 //sleep(10000) } //use this below can be go to Logout directly testRunner.runTestStepByName("DeleteAPI1") testRunner.gotoStepByName("Logout") //use below to clear public data testcase.testSteps["DeleteService1"].run(testRunner,context) testcase.testSteps["DeleteService2"].run(testRunner,context) testcase.testSteps["DeleteService3"].run(testRunner,context) testcase.testSteps["GetService1_twice"].run(testRunner,context) testcase.testSteps["GetService2_twice"].run(testRunner,context) testcase.testSteps["GetService3_twice"].run(testRunner,context) testcase.testSteps["Logout"].run(testRunner,context) case0: 在没有任何前提条件下直接调用被测API case1:前提条件是数据库中已经有Service1的数据(数据的需求按照需求文档来) case2:前提条件是数据库中已经有Service1和Service2的数据(数据的需求按照需求文档来) case3:前提条件是数据库中已经有Service1和Service2 和S ervice3的数据(数据的需求按照需求文档来) 执行完之后对被测API的数据进行删除 testRunner.runTestStepByName("DeleteAPI1") 执行完之后对前提条件准备的所有数据进行删除 <pre name="code" class="java">testcase.testSteps["DeleteService1"].run(testRunner,context) note:以上例子是以前提很简单的组合,如果有更为复杂的组合可以自己根据需求增加PublicCase. 调用另一个TestSuite中的teststep脚本如下: def testproject = testRunner.testCase.testSuite.project def testcase = testproject.testSuites['PublicCase'].testCases['TestCase1'] testcase.testSteps["CreateService1"].run(testRunner,context) 调用本TestSuite中的TestStep脚本如下: testRunner.runTestStepByName("Login") testRunner.runTestStepByName("CreateAPI1-Scenario2") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |