今天看了一个借助Groovy语言实现的行为驱动框架-easyb,感觉很有趣,简单总结一下跟大家分享:
easyb 是基于Groovy的DSL实现的可适用于Java和Groovy的测试框架。它提供了对Ant和Maven的支持来执行stories测试。(下面的例子我们以Ant为例)
它的目的就是: 让我们的单元测试跟接近于业务语言,为此他提供了三个关键字:given, when, then。见名知义,given是提供一个上下文,when是提供一个业务场景,then是执行一些断言。
如何开始呢?
1、首先我们需要从官方下载easyb发布包,它包括commons-cli-VERSION.jar, easyb-VERSION.jar, groovy-VERSION.jar,将这些依赖引入到你的项目中;
2、编写你的业务接口和实现类,这些步骤与平常无异,说明省略;
3、定义build.xml,启动easyb:
<?xml version="1.0"?> <project name="easyb" basedir="." default="all"> <property name="src.dir" value="src" /> <property name="lib.dir" value="lib" /> <property name="target.dir" value="target" /> <property name="classes.dir" value="bin" /> <property name="behaviors.dir" value="stories" /> <property name="test.dir" value="test" /> <path id="easyb.classpath"> <fileset file="${lib.dir}/*.jar" /> </path> <taskdef name="easyb" classname="org.disco.easyb.ant.BehaviorRunnerTask"> <classpath> <path refid="easyb.classpath" /> </classpath> </taskdef> <target name="init"> <delete dir="${target.dir}"/> </target> <target name="all" depends="init"> <mkdir dir="${target.dir}"/> <easyb failureProperty="easyb.failed"> <classpath> <path refid="easyb.classpath" /> <pathelement path="${classes.dir}" /> </classpath> <behaviors dir="${behaviors.dir}"> <!-- Discounts.story and DiscountsStory.groovy are both valid name --> <include name="*.story"/> </behaviors> <report location="${target.dir}/story.txt" format="txtstory"/> <report location="${target.dir}/story" format="txtspecification"/> <report location="${target.dir}/story.xml" format="xml"/> </easyb> </target> <fail if="easyb.failed" message="easyb reported a failure"/> </project>
从上面的XML文件中我们可以看到easyb首先在ANT中注册它的Task,然后调用easyb,测试用例由*Story.groovy或者*.story提供,report生成报告,支持多种格式输出。
4、下面看看easyb的魔力所在:
import com.samueli.service.AccountServiceImpl; import com.samueli.service.LoginServiceImpl; scenario "User enters valid credentials", { given "user account already exists",{ accountService = new AccountServiceImpl() loginService = new LoginServiceImpl() loginService.setAccountService(accountService) } when "user logins",{ account = loginService.login("meera", "password") } then "the system returns a valid account",{ account.getUserid().shouldBe "meera" account.getPassword().shouldBe "password" } } scenario "User enters invalid credentials", { given "user account already exists",{ accountService = new AccountServiceImpl() loginService = new LoginServiceImpl() loginService.setAccountService(accountService) } when "user logins with invalid password",{ account = loginService.login("meera", "meera") } then "a null account should be returned",{ account.shouldBe null } } scenario "Invalid login with a null password", { given "user account already exists",{ accountService = new AccountServiceImpl() loginService = new LoginServiceImpl() loginService.setAccountService(accountService) } when "user logins with null password", { enternull = { loginService.login("meera", null) } } then "an exception should be thrown", { ensureThrows(BusinessException.class){ enternull() } } }
5、上面的测试利用了easyb的三个关键词,模拟了一系列业务场景和断言。
我们运行ANT,可能会得到这样的运行结果:
Buildfile: E:\MyProject\TestEasyb\build.xml init: [delete] Deleting directory E:\MyProject\TestEasyb\target all: [mkdir] Created dir: E:\MyProject\TestEasyb\target [easyb] easyb is preparing to process 1 file(s) [easyb] Running login service test story (LoginServiceTest.story) [easyb] Scenarios run: 3, Failures: 0, Pending: 0, Time Elapsed: 0.828 sec [easyb] 3 total behaviors run with no failures [easyb] easyb execution passed BUILD SUCCESSFUL Total time: 2 seconds
具体的业务代码,我们可以参考附件,Good Luck!
参考:
http://groovy.dzone.com/articles/is-easyb-easy
https://www6.software.ibm.com/developerworks/education/j-easyb/section2.html