今天看了一个借助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
分享到:
相关推荐
“EasyB”企业信息化综合解决方案.pdf“EasyB”企业信息化综合解决方案.pdf“EasyB”企业信息化综合解决方案.pdf“EasyB”企业信息化综合解决方案.pdf“EasyB”企业信息化综合解决方案.pdf“EasyB”企业信息化综合...
【标题】"Thucydides-EasyB-Archetype-0.8.26.zip" 是一个开源项目,其中包含了创建Thucydides和EasyB集成的Maven项目模板。Thucydides是一个用于Web应用程序端到端测试的Java框架,而EasyB则是一种用自然语言风格...
maven-easyb-plugin-1.4.jar
maven-easyb-plugin-1.3.jar
maven-easyb-plugin-1.2.jar
maven-easyb-plugin-1.1.jar
maven-easyb-plugin-0.9.jar
maven-easyb-plugin-0.9.7.jar
maven-easyb-plugin-0.9.6.jar
maven-easyb-plugin-0.9.5.jar
maven-easyb-plugin-0.9.2.jar
maven-easyb-plugin-0.9.1.jar
maven-easyb-plugin-0.8.jar
maven-easyb-plugin-0.7.jar
本资源"PyPI 官网下载 | easyb-0.1.0.0-py3-none-any.whl"正是来自于PyPI的一个特定版本的Python库——easyb。 "easyb-0.1.0.0-py3-none-any.whl"是一个轮子文件(wheel file),它是Python包的一种二进制格式,...
maven-easyb-plugin-1.4-sources.jar
maven-easyb-plugin-1.3-sources.jar
maven-easyb-plugin-1.2-sources.jar
maven-easyb-plugin-1.1-sources.jar