1

SoapUI:快放过我吧...

不!你是YYDS😼

由于SoapUI的SOAP MockService限制较多,所以这套规范只使用REST MockService

规范要点

  • 被动执行的一定是TestCase:这是因为在SoapUI里,TestCase的可塑性较强,实施人员可以更灵活地按需求进行设置。
  • TestCase中一定要有一个名为Properties的Properties步骤且通常在第一位,其中必须创建result、message和data这三个变量,这几个变量在其他Test Step将会被使用到,对MockService默认Response的规范Script来说也是必须的变量。
  • 通常来说,TestStep中每个步骤之间都是用Property Transfer来传值的,并且最后一个步骤也通常是使用Property Transfer来给必须的三个变量传值的,文章下方的范例中有对这三个必须变量的XPath表达式进行了示范。
  • 为MockService创建的Action默认为GET,GET对传入参数有一定限制,方便使用浏览器地址栏就能测试外,这一点限制恰好又顺应了规范要求:传入较少的参数,再通过该参数在TestCase内部来得到更详细的内容,而不是从传参开始就传很多内容。
  • 默认Response规范的Content和Script,日后应该还会有一定的调整,目前可以用来测试参考一下:
    Content:

    {
      "result": "${result}",
      "message": "${message}",
      "data": "${data}"
    }

    Script:

    // 参数设置
    def testSuiteStr = "TestSuite 1"
    def testCaseStr = "TestCase 2"
    
    // 固定脚本
    def project = mockResponse.mockOperation.mockService.project
    def testCase = project.testSuites[testSuiteStr].testCases[testCaseStr]
    def request = mockRequest.getHttpRequest()
    
    def names = request.getParameterNames()
    for (name in names)  {
      def value = request.getParameter(name)
      testCase.testSteps["Properties"].setPropertyValue(name, value)
    }
    
    def runner = testCase.run( new com.eviware.soapui.support.types.StringToObjectMap(), false )
    
    if( requestContext.status = runner.status.toString() == "FINISHED" ) {
      requestContext.result = testCase.testSteps["Properties"].getPropertyValue("result")
      requestContext.message = testCase.testSteps["Properties"].getPropertyValue("message")
      requestContext.data = testCase.testSteps["Properties"].getPropertyValue("data")
    }
    else {
      requestContext.result = "Failed"
      requestContext.message = runner.reason
    }

    通常来说,实施人员只需要设置该脚本最上面的参数设置填入项目的TestSuite和TestCase名称即可

范例

  1. 创建一个传参并返回结果的简单WebMethod如下(这里使用的是asp.net):

    [WebMethod]
    public float Division(float a, float b)
    {
     return a / b;
    }
  2. 解析入SoapUI:
    image.png
  3. 为该请求创建一个新的TestCase:
    image.png
  4. 在TestCase步骤中加入一个Properties,并添加上相关变量:
    image.png
  5. 在请求步骤以${变量名}的格式使用变量:
    image.png
  6. 在TestCase步骤中加入一个Property Transfer,并为规范中规定的必须变量逐一创建Transfer,Target设为对应的必须变量,再Source中输入相应的XPath表达式:
    result:

    declare namespace tem="http://tempuri.org/";
    if (//tem:DivisionResult)
    then "Success"
    else 'Failed'

    message:

    //faultstring

    data:

    declare namespace tem="http://tempuri.org/";
    //tem:DivisionResult

    image.png

  7. 右键点击Project,创建一个REST MockService,设置Service端口号及虚拟目录:
    image.png
  8. 为REST MockService创建一个GET Action,并为该Action创建一个默认的Response:
    image.png
  9. 为默认Response按规范设置好Content及Script,如下:
    image.png
  10. 启动MockService:
    image.png
    再使用浏览器测试下该服务:
    image.png
    image.png
    尝试关闭下WebService,得到结果:
    image.png

latte6six
10 声望1 粉丝

引用和评论

0 条评论