重生之世家子弟续:easyb的使用

来源:百度文库 编辑:九乡新闻网 时间:2024/05/07 12:56:55

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-0.9.8.jar;lib\commons-cli-1.2.jar;lib\groovy-all-1.7.5.jar;bin\ org.easyb.BehaviorRunner bin\LoginServiceTest.story bin\HelloService.story bin\IOService.story

 

%easyb_home%>java -cp easyb-0.9.8.jar;lib\commons-cli-1.2.jar;lib\groovy-all-1.7.5.jar org.easyb.BehaviorRunner Noname.specification

 

Colored command line output

% Project Path %>java -cp lib\easyb-0.9.8.jar;lib\commons-cli-1.2.jar;lib\groovy-all-1.7.5.jar;bin\ org.easyb.BehaviorRunner bin\LoginServiceTest.story bin\HelloService.story bin\IOService.story -prettyprint

 

easyb plug-in for Eclipse 运行easyb:

 

1、  Eclipse中更新Easyb的plug-in(Help->Install New Software...):

         Groovy:http://dist.springsource.org/release/GRECLIPSE/e3.5/

         Easyb:http://easyb.googlecode.com/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

 

  

       D:\Java\MavenRepo  

   ...

 

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=0.9.7-1 -Dpackaging=jar -Dfile=maven-easyb-plugin-0.9.7-1.jar

         %DfilePath%>mvn install:install-file -DgroupId=org.easyb -DartifactId=easyb -Dversion=0.9.7 -Dpackaging=jar -Dfile=easyb-0.9.7.jar

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

 

   

      org.easyb

      maven-easyb-plugin

      0.9.7-1

      test

   

 

 

   

     

        org.easyb

        maven-easyb-plugin

        0.9.7-1

       

         

           

              test

           

         

       

     

   

 

Customized Directories

 

   

     

        org.easyb

        maven-easyb-plugin

        0.9.7-1

       

         

           

              test

           

         

       

       

          ${basedir}/src/test/stories

          ${project.build.directory}/easyb-stories.txt

          ${project.build.directory}/easyb-report.xml

       

     

   

 

Creating HTML story reports

 

   

     

        org.easyb

        maven-easyb-plugin

        0.9.7-1

       

         

           

              test

           

         

       

        

${basedir}/src/test/stories

          html

          ${project.build.directory}/easyb/stories.html

       

     

    

 

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:

 

 

 

   

     

        org.easyb

        maven-easyb-plugin

        0.9.7-1

       

         

           

              test

           

         

       

       

            runme,metoo

       

     

   

 

 

 

 

参考网址

 

http://www.easyb.org/

 

http://www.easyb.org/maven-easyb-plugin/