easyb 是基于Groovy的DSL(Domain Specific Languages领域定义语言)实现的可适用于Java和Groovy的测试框架。它提供了对Ant和Maven的支持来执行stories测试。 它的目的就是: 让我们的单元测试跟接近于业务语言,为此他提供了三个关键字:given, when, then。见名知义,given是提供一个上下文,when是提供一个业务场景,then是执行一些断言。 Ant运行easyb: 1. 首先需要从官方下载easyb发布包,它包括commons-cli-VERSION.jar, easyb-VERSION.jar, groovy-VERSION.jar,将这些依赖引入到你的项目lib文件夹中。 2. 编写业务接口和实现类,Java代码如下可参考。
3. 定义build.xml,启动easyb,Xml代码如下,可参考。
从上面的XML文件中我们可以看到easyb首先在ANT中注册它的Task,然后调用easyb,测试用例由*Story.groovy或者*.story提供,report生成报告,支持多种格式输出。 4. 下面看看easyb的魔力所在:Groovy代码如下,可参考。
5. 上面的测试利用了easyb的三个关键词,模拟了一系列业务场景和断言。我们运行ANT(打开Ant视图或者在build.xml 文件上右击,run as),可以得到这样的运行结果,文本信息代码如下: Buildfile: D:\talendProgramWorkspace\EasybDemo\build.xml init: [delete] Deleting directory D:\talendProgramWorkspace\EasybDemo\target all: [mkdir] Created dir: D:\talendProgramWorkspace\EasybDemo\target [easyb] easyb is preparing to process 3 file(s) [easyb] Running hello service story (HelloService.story) [easyb] Scenarios run: 1, Failures: 0, Pending: 0, Time elapsed: 0.593 sec [easyb] Running IO service story (IOService.story) [easyb] Scenarios run: 2, Failures: 0, Pending: 0, Time elapsed: 0.079 sec [easyb] Running login service test story (LoginServiceTest.story) [easyb] Scenarios run: 3, Failures: 0, Pending: 0, Time elapsed: 0.156 sec [easyb] 6 total behaviors ran with no failures [easyb] easyb execution passed BUILD SUCCESSFUL Total time: 2 seconds Command line运行easyb: %Project Path %>java -cp lib\easyb- %easyb_home%>java -cp easyb- Colored command line output % Project Path %>java -cp lib\easyb- easyb plug-in for Eclipse 运行easyb: 1、 Eclipse中更新Easyb的plug-in(Help->Install New Software...): Groovy:http://dist./release/GRECLIPSE/e3.5/ Easyb:http://easyb./svn/trunk/eclipse-plugins/org.easyb.eclipse.updatesite/ 2、 创建Java Project,Add Easyb Libraries (Right-click Java Project->build path-> Add Libraries ->User Library->new &Add Jars…) 。 3、 编写业务接口和实现类。 4、 创建easyb story,Right-click Java Project->other->Easyb中的Story,finish。 5、 在story中编写测试用例,Right-click story->Run as->Behaviour,即可运行easyb。 或者 创建Groovy Project,编写业务接口和实现类。创建easyb story,Right-click Java Project->other->Easyb中的Story,finish。在story中编写测试用例,Right-click story->Run as->Behaviour,即可运行easyb。 Modify maven repository path: 1.%MAVEN_HOME%\conf\settings.xml <settings> <!-- localRepository | The path to the local repository maven will use to store artifacts. | Default: ~/.m2/repository <localRepository>/path/to/local/repo </localRepository> --> <localRepository>D:\Java\MavenRepo</localRepository> ... </settings> 2.eclipse->window->preferences->maven->user setting (%MAVEN_HOME%\conf\settings.xml). Maven运行easyb: Maven jar包安装只需执行一次即可。第3步只执行一次即可 1.创建maven工程:%Workspace%>mvn archetype:create -DgroupId=com.test -DartifactId=EasybMvnDemo 2.maven工程转化为eclipse工程:%ProjectPath%>mvn eclipse:eclipse 3.安装jar包 %DfilePath%>mvn install:install-file -DgroupId=org.easyb -DartifactId=maven-easyb-plugin -Dversion= %DfilePath%>mvn install:install-file -DgroupId=org.easyb -DartifactId=easyb -Dversion= 4.导入该eclipse工程。 5.在src/main/java下面的com.test包中编写接口和实现类 在src\test\easyb下面编写story或specification文件即可 Dependency的maven-easyb-plugin.jar 需写入pom.xml文件。 6.Project Pom.xml Basic Configuration <dependencies> <dependency> <groupId>org.easyb</groupId> <artifactId>maven-easyb-plugin</artifactId> <version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.easyb</groupId> <artifactId>maven-easyb-plugin</artifactId> <version> <executions> <execution> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin> </plugins> </build> Customized Directories <build> <plugins> <plugin> <groupId>org.easyb</groupId> <artifactId>maven-easyb-plugin</artifactId> <version> <executions> <execution> <goals> <goal>test</goal> </goals> </execution> </executions> <configuration> <easybTestDirectory>${basedir}/src/test/stories</easybTestDirectory> <storyReport>${project.build.directory}/easyb-stories.txt</storyReport> <xmlReport>${project.build.directory}/easyb-report.xml</xmlReport> </configuration> </plugin> </plugins> </build> Creating HTML story reports <build> <plugins> <plugin> <groupId>org.easyb</groupId> <artifactId>maven-easyb-plugin</artifactId> <version> <executions> <execution> <goals> <goal>test</goal> </goals> </execution> </executions> <configuration> <easybTestDirectory>${basedir}/src/test/stories</easybTestDirectory> <storyType>html</storyType> <storyReport>${project.build.directory}/easyb/stories.html</storyReport> </configuration> </plugin> </plugins> </build> Running stories with specific tags Tags can be activated via the 'easyb.tags' command line flag (e.g. "-Deasyb.tags='runme,metoo'") or by configuring them in the pom.xml: <build> <plugins> <plugin> <groupId>org.easyb</groupId> <artifactId>maven-easyb-plugin</artifactId> <version> <executions> <execution> <goals> <goal>test</goal> </goals> </execution> </executions> <configuration> <tags>runme,metoo</tags> </configuration> </plugin> </plugins> </build> 参考网址 http://www./ http://www./maven-easyb-plugin/ |
|