基于 SoapUI 工具测试 Rest 服务

来源:互联网 发布:python bytebuffer 编辑:程序博客网 时间:2024/05/21 18:27

简介
  SoapUI 是一个免费的、开源的、跨平台的测试工具。支持多种协议,例如:SOAP, REST, HTTP, JMS, AMF 和 JDBC。通过 SoapUI 可以方便快速地进行自动化测试及性能测试。
  
1. download url and doc 
   https://www.soapui.org/downloads/latest-release.html 
   https://sourceforge.net/projects/soapui/files/soapui/5.2.1/ 
   
2. 通过 SoapUI 测试 Rest 接口
2.1 准备

  下文开始演示通过 SoapUI 如何测试 REST 接口,在开始之前,可参考以下链接搭建一个 REST 接口的服务端用于测试:
  # 基于 Resteasy 框架搭建 Rest 服务( resteasy-jaxrs-3.0.13.Final )
  https://code.csdn.net/snippets/1614440/master/snippet_file_0.txt/raw 

  # 基于 Jersey 框架搭建 Rest 服务( Jersey 2.22.2 )
  https://code.csdn.net/snippets/1633225/master/snippet_file_0.txt/raw 
    
# SoapUI 中的自定义属性
语法 : ${[scope]propertyName[#xpath-expression]} 
  Projects  级别 : ${#Project#propertyName}
  TestSuite 级别 : ${#TestSuite#propertyName}
  TestCases 级别 : ${#TestCase#propertyName}
参考链接 
  https://www.soapui.org/scripting---properties/property-expansion.html 
  
2.2 通过 SoapUI 测试登录接口
2.2.1 创建 Project
 

# 这里输入登录接口的 url (任意一个 Rest 接口的 url 都可以)


2.2.2  测试登录接口


2.2.3 创建测试用例


 



2.2.4 校验  登录接口  用例执行结果





# 将登录接口返回的 token 和 sessionId 信息保存到变量中

def tokenStr = messageExchange.responseHeaders["token"][0];def cookieList = messageExchange.responseHeaders["Set-Cookie"];def respText = messageExchange.responseContent;def sessionId = "";// get sessionId from header for (def newCookie : cookieList) {    def start = newCookie.indexOf("JSESSIONID=");    if (start == -1) {        continue;    }    def end = newCookie.indexOf(";", start + 1);    if (end == -1) {        end = newCookie.length();    }    sessionId = newCookie.substring(start + "JSESSIONID=".length(), end);    break;}log.info "token = **" + tokenStr + "**";log.info "sessionId = **" + sessionId + "**";log.info "respText = **" + respText + "**";// save token and sessionId to testCase propertymessageExchange.modelItem.testStep.testCase.setPropertyValue("token", tokenStr);messageExchange.modelItem.testStep.testCase.setPropertyValue("cookie", "JSESSIONID=" + sessionId);// end 

参考链接
  https://www.soapui.org/functional-testing/validating-messages/using-script-assertions.html 



2.2.5 添加查询用户的 Rest 接口





  









# 在日志中输出接口返回的用户信息

def jsonUtils = new groovy.json.JsonSlurper();def respText = messageExchange.responseContent;def jsonObj = jsonUtils.parseText(respText);def userList = jsonUtils.parseText(jsonObj.data);def firstUser = userList[0];log.info "id = " + firstUser.id + ", name = " + firstUser.name;// json obj to string // def jsonStr = new groovy.json.JsonBuilder(jsonObj).toPrettyString();



2.2.6 添加注销的 Rest 接口








##################################################################################################

2.3 通过 SoapUI 的 Mock Service 功能模拟 Rest 接口的服务端

2.3.1 新建一个 Mock Service



2.3.2 设置 Mock Service 的路径和端口


2.3.3 添加登录接口






2.3.4 添加查询用户接口





2.3.5 添加注销接口





2.3.6 启动 Mock Service



2.3.7 修改测试用例访问的 url (修改为 Mock Service 的 url)





2.3.8 执行测试用例来调用 Mock Service




# 在 Groovy Script 中获取/设置属性 
testRunner.testCase.getPropertyValue("MyProp")
testRunner.testCase.setPropertyValue("MyProp", someValue)
# 在 Script Assertion 中获取/设置属性 
messageExchange.modelItem.testStep.testCase.getPropertyValue("MyProp")
messageExchange.modelItem.testStep.testCase.setPropertyValue("MyProp", someValue)
# mock service 中获取 request 
def req = mockRequest.getHttpRequest()
#  
https://www.soapui.org/scripting-properties/tips-tricks.html 

http://pan.baidu.com/s/1dE0AhbF#path=%252Fimgs%252FSoapUI%252FSoapUI 

# end

2 0
原创粉丝点击