场景

在进行Web服务端开发的时候,发布前通常需要测试一遍。对于一个大一点的项目,最好的办法是写个自动化测试程序。

以Groovy为例,写测试代码之前通常的有如下几个操作

  1. 引用相关的类库
  2. import相关的类
  3. 对库不熟悉的时候你很可能得先把库的文档好好看一遍

对于你来说,你需要的可能仅仅是post,get等几个简单的操作而已,而上面的操作更是可能占用你整个开发过程的大部分时间。

Orz....项目进度没跟上,又要加班了。。。。

要是有一种语言,本身自带post,get这样的函数那该多好啊,测试程序哗啦哗啦就写完了!

解决方案

通过Groovy构建一个脚本环境,自带post,get这些常用的函数

效果

再也不用手动引用库了,再也不用手动import类了,再也不用复习好长好长的文档了,写测试脚本再也腰不疼、腿不麻了!

原理

groovy本身是一个强大的脚本引擎,同时也是高度可定制化的。

在groovy编译脚本的时候,可以为脚本指定一个基类,这个基类的所有方法和属性都可以直接在脚本引用。

实现

首先,先新建一个工程,这里用gradle作为构建工具,取工程名为httpbatch

新建build.gradle

apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'application'
mainClassName = 'com.kasonyang.httpbatch.Application'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.10'
compile 'org.apache.httpcomponents:httpclient:4.5'
}

工程引用了apache的httpclient组件,并指定主类为com.kasonyang.httpbatch.Application

生成Eclipse工程

$ gradle eclipse

然后我们就可以使用Eclipse导入工程了。

创建主类Application.groovy

package com.kasonyang.httpbatch
import com.kasonyang.httpbatch.test.TestException
import org.codehaus.groovy.control.CompilerConfiguration
class Application { static void printUsage(){
println """
usage : httpbatch file #execute a file
or httpbatch -s #enter shell mode
"""
} static void main(String[] args) {
if(args.length<1){
printUsage()
return
}
def reader,scriptStr = ''
switch(args[0]){
case '-s':
print ">"
reader = System.in
reader.eachLine {it->
scriptStr += it + '\n'
if(it == ''){
runScript(scriptStr)
scriptStr = ''
}//else{
print '>'
//}
}
break
default:
def file = new File(args[0])
runScript(file)
break
}
} private static String getExceptionStack(Exception ex,String clsName){
String msg = ""
for(stack in ex.stackTrace){
if(stack.className == clsName){
def fileName = stack.fileName
def lineNumber = stack.lineNumber.toString()
msg += ("\tFile:${fileName}(${lineNumber})")
}
}
msg
} private static runScript(def it){
def config = new CompilerConfiguration()
config.scriptBaseClass = 'com.kasonyang.httpbatch.HttpBatchScript'
def shell = new GroovyShell(config)
def scriptClass
try{
//shell.evaluate(file)
def script = shell.parse(it)
scriptClass = script.class.name
def stateReturn = script.run()
//System.out.println(stateReturn)
}catch(TestException ex){
println "test fail:"
println getExceptionStack(ex,scriptClass)
println "\texcepted:${ex.excepted}\n\tactual:${ex.actual}"
}catch(Exception ex){
println ex.message
println getExceptionStack(ex,scriptClass)
}catch(RuntimeException ex){
println ex.message
println getExceptionStack(ex,scriptClass)
}
}
}

Application指定了脚本的基类为com.kasonyang.httpbatch.HttpBatchScript,这个类就是主题的主体。

先上代码

package com.kasonyang.httpbatch
import com.kasonyang.httpbatch.test.TestException
import com.kasonyang.httprequest.HttpRequest
import com.kasonyang.httprequest.HttpResponse
import groovy.lang.Binding
import groovy.lang.Script;
abstract class HttpBatchScript extends Script { private http = new HttpRequest();
HttpResponse $$ Closure beforeGo,beforePost,afterGo,afterPost Closure testFail private String base = ''
private String path = '' private String trimPath(String path,boolean left=true,boolean right=true){
int start =(left && path.startsWith('/')) ? 1 : 0;
int end = (right && path.endsWith('/')) ? path.length()-1 : path.length();
path.substring(start,end)
} private def getUri(uri){
base + (base?'/':'') + path + (path?'/':'') + trimPath(uri,true,false)
} def base(){
this.base = ''
} /**
* set the base path of request
* @param path the base path
* @return
*/
def base(String path){
this.base = trimPath(path)
} def enter(){
this.path = ''
} /**
* enter a directory in base
* @param path
* @return
*/
def enter(String path){
this.path = trimPath(path)
} /**
* submit a get request
* @param uri the request uri
* @param params the query params
* @param callback call after request
* @return
*/
HttpResponse go(String uri,Map params,Closure callback){
def httpGet = http.createGet(getUri(uri),params)
this.beforeGo?.call(httpGet)
def response = http.execute(httpGet)
this.$$ = response
if(callback) callback.call()
this.afterGo?.call(response)
return this.$$
}
HttpResponse go(String uri,Closure callback){
return go(uri,[:],callback)
}
HttpResponse go(String uri,Map params){
return go(uri,params,null)
}
HttpResponse go(String uri){
return this.go(uri,null)
} /**
* submit a post request
* @param uri the request uri
* @param params the post params
* @param callback call after request
* @return
*/
HttpResponse post(String uri,Map params,Closure callback){
def httpPost = http.createPost(getUri(uri),params)
this.beforePost?.call(httpPost)
def response = http.execute(httpPost)
this.$$ = response
if(callback) callback.call()
this.afterPost?.call(response)
return this.$$
}
HttpResponse post(String uri,Closure callback){
return post(uri,[:],callback)
}
HttpResponse post(String uri,Map params){
return post(uri,params,null)
}
HttpResponse post(String uri){
return this.post(uri,null)
} /**
* set the beforeGo callback,which whill be call before every get request
* @param callback
*/
void beforeGo(Closure callback){
this.beforeGo = callback
} /**
* set the beforePost callback,which whill be call before every post request
* @param callback
*/
void beforePost(Closure callback){
this.beforePost = callback
} /**
* set the callback,which whill be call when test fail
* @param cb
*/
void testFail(Closure cb){
this.testFail = cb
} /**
* set the callback,which whill be call after every get request
* @param callback
*/
void afterGo(Closure callback){
this.afterGo = callback
} /**
* set the callback,which whill be call after every post request
* @param callback
*/
void afterPost(Closure callback){
this.afterPost = callback
} /**
* test whether it is true
* @param value
*/
void testTrue(Object value){
testEquals(true,value)
} /**
* test whether actual equals the excepted
* @param excepted
* @param actual
*/
void testEquals(Object excepted,Object actual){
if(excepted != actual){
def ex = new TestException(excepted,actual)
if(this.testFail){
testFail(ex)
}else{
throw ex
}
}
} /**
* test whether it is null
* @param value
*/
void testNull(Object value){
testEquals(null,value)
} }

这个类主要定义了一个public属性$$还有几个public方法,也就是post、go,和一些其它可能需要用到的函数。

因为get方法在groovy里有特殊意义,这里使用go方法名代替了get。

提示:这里使用了另外两个类,HttpRequest和HttpResponse,是自定义的两个Class,由于篇幅的原因,这里就不再贴代码了,具体实现可前往Github查看。

我已经把全部源码放到了Github,感兴趣的可以前往查看。

地址:https://github.com/kasonyang/httpbatch

构建项目

$ gradle installDist

程序被输出到build/install/httpbatch目录下,将bin目录添加到环境变量PATH中。

使用

  • 创建脚本文件 "example.hb"
go "YOU_URL"//对你要测试的URL提交get请求
testEquals 200,$$.statusCode//状态码为 200?
def text = $$.text //get the response as text
def json = $$.json//get the response as json
println text //output the response
//这里添加你的测试逻辑代码
println "Test successfully!"
  • 执行脚本文件
$ httpbatch example.hb

使用Groovy构建自己的脚本环境的更多相关文章

  1. Spark:利用Eclipse构建Spark集成开发环境

    前一篇文章“Apache Spark学习:将Spark部署到Hadoop 2.2.0上”介绍了如何使用Maven编译生成可直接运行在Hadoop 2.2.0上的Spark jar包,而本文则在此基础上 ...

  2. 为 Python Server Pages 和 Oracle 构建快速 Web 开发环境。

    为 Python Server Pages 和 Oracle 构建快速 Web 开发环境. - 在水一方 - 博客频道 - CSDN.NET 为 Python Server Pages 和 Oracl ...

  3. 使用Eclipse+Maven+Jetty构建Java Web开发环境(几个教程综合集成2014发行)

    工作需要使用Jetty由于web集装箱,得知Eclipse+Maven+Jetty该组合是非常好的,因此,要在网上找了很多教程,但不写或多或少特定的或过时的内容而导致最终的配置失败,易于配置为未来的同 ...

  4. [phvia/dkc] Docker Compose 快速构建(LNMP+Node)运行环境

    快速构建(LNMP+Node)运行环境. dkc 在此作为 docker-compose 的缩写,你可以理解为 alias dkc=docker-compose 准备 安装 docker 选择1) 从 ...

  5. [转]构建Python+Selenium2自动化测试环境(二)

    构建Python+Selenium2自动化测试环境完成之后,就需要测试支持python的selenium的版本是否都支持在不同浏览器上运行,当前我们分别在三个最通用的浏览器上通过脚本来测试. 1.在I ...

  6. [.net 面向对象程序设计深入](5)MVC 6 —— 构建跨平台.NET开发环境(Windows/Mac OS X/Linux)

    [.net 面向对象程序设计深入](5)MVC 6 —— 构建跨平台.NET开发环境(Windows/Mac OS X/Linux) 1.关于跨平台 上篇中介绍了MVC的发展历程,说到ASP.NET ...

  7. 快速构建springmvc+spring+swagger2环境

    快速构建springmvc+spring+swagger2环境 开发工具:Intellij idea               jdk: 1.8 开发步骤: 1.创建maven工程,如图建立工程结构 ...

  8. Linux计划任务 定时任务 Crond 配置详解 crond计划任务调试 sh -x 详解 JAVA脚本环境变量定义

    一.Crond 是什么?(概述) crontab 是一款linux系统中的定时任务软件用于实现无人值守或后台定期执行及循环执行任务的脚本程序,在企业中使用的非常广泛.     现在开始学习linux计 ...

  9. Android NDK开发 Android Studio使用新的Gradle构建工具配置NDK环境(一)

    本文主要讲述了如何如何在Android Studio使用新的Gradle构建工具配置NDK环境,现在把相关的步骤整理出来分享给Android程序员兄弟们,希望给他们在配置NDK环境时带来帮助. 从An ...

随机推荐

  1. C#使用Cookie方法

    //写入 protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie=new HttpCookie(&qu ...

  2. README.md

    #gzq-h5app 本项目依赖于nodejs,请尽量安装最新版本. 需要先将Grunt命令行(CLI)安装到全局环境中.安装时可能需要使用sudo(针对OSX.*nix.BSD等系统中)权限或者作为 ...

  3. iPhone 6 (iOS 9.2) extractiion failed by XRY

    My colleague extracted an iPhone 6 with XRY and it is iOS 9.2 . Unfortunately the Wizard crashed and ...

  4. JSON-JQuery常用技巧

    1:Jquery对象选择查找 var group = $(".classeslist li"); class 为 classeslist 内部 的所有 li 元素对象 遍历: fo ...

  5. 设计模式-工厂方法模式(FactoryMethod)

    简介: 简单工厂模式将类的示例化放在工厂对象中. 工厂方法模式是简单工厂模式的延伸,不同的是其将子类的实例化延迟到子类工厂中实现,本身仅定义一个创建对象的接口. 工厂方法模式主要由四部分组成: 1.抽 ...

  6. 《你是我的小羊驼》游戏源码 v1.0

    游戏分析三个界面基本上就是整个游戏的全部内容:1.左边的是主界面,展示游戏名称以及主角,让玩家对游戏的整体画风有个大概的印象.2.中间的是游戏界面,点击空格防止橙色六边形砖块来围堵小羊驼.3.右边的是 ...

  7. 一款安卓ShowcaseView视图源码效果

    该源码是从源码天堂那边转载过来的,大家可以看看一下吧啊,一款安卓ShowcaseView视图源码效果,非常不错的,特别是在做引导时使用. 源码下载地址:http://code.662p.com/vie ...

  8. 安装JAVA JDK注意事项

    1 WIN8系统 安装JDK  我安装的是1.6版本,   64位的 : 2 安装好后  cmd  输入 java -version  正常 java  命令  正常 javac 命令不正常    提 ...

  9. 关于javascript 里面类型的判断

    javacript至今共有7中类型 Six data types that are primitives: Boolean Null Undefined Number String Symbol (n ...

  10. recurse_array_change_key_case()递规返回字符串键名全为小写或大写的数组

    //递归返回字符串键名全为小写或大写的数组function recurse_array_change_key_case(&$input, $case = CASE_LOWER){    if( ...