JAVA自动化单元测试工具Junit Factory使用介绍

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

JA V A自动化单元测试工具Junit Factory使用介绍(开发人员不再需要编写Junit测试用例)
最近在PassPort项目中,很多开发人员都采用了Junit作为单元测试工具,保证代码质量,所以我最近研究了agitar公司的一种免费的junit自动生成的工具-junit factory,agitar公司有一套更优秀的商业软件---自动化单元测试工具agitarone。

Junit是JA V A开发中应用最多的单元测试框架,部门内部也逐渐普及到各个项目开发中,虽然Junit测试框架易学易用,但是还是在保证代码质量的同时增加了开发人员的代码工作量,需要拿出部分时间编写Junit测试代码。

Junit Factory就能更好的提高单元测试效率!
原理:
Junit Factory和agitarone的一项关键技术就是software agitation,它可以为你自动创建各种测试用例(实际测试必须要测试每一行、每一条分支、甚至每一种输出),而且自动创建的过程是通过对代码的agitation,(一种基于java二进制代码的分析,可以自动得出需要测试的东东)后跟开发者交互来完成的。

agitation的这种技术是基于服务器并且是分布式的,可以远程使用,并可以利用多个服务器来完成。

安装:
Junit Factory是可以作为eclipse插件使用的,也可以和ant一起使用。

部门内主要开发工具是eclipse,所以介绍如何在eclipse下用Junit Factory生成Junit单元测试用例!
Download :/update/.
1.Eclipse, 选Help ,选Software Updates ,点Find and Install ;
2.选择“Search For New Features to Install”然后“Next”;
3. 选New Remote Site ;
4. 输入一个JUnit Factory网站的注册用户名;
5. 输入url:/update/ ;
6. 安装所有plug-ins ;
7. 重新启动eclipse ;
使用:
安装完成后会在eclipse的工具条中出现Generate Tests和Generate Dashboard两个按钮,
使用Generate Tests生成Junit单元测试用例,使用Generate Dashboard查看包或者项目的单元测试结果和带代码分析报告(利用Crap4J进行代码分析,得到Fail数量、Pass数量、覆盖率、代码规范错误、Risk Classes等等)。

操作很简单,不详述!
如图1
源码和生成的Junit测试代码举例
源码源码:
/*
Instructions:
1. Enter (or paste) some code
2. Hit "Generate a Test"
3. Y our test will appear in the queue on the right of the page
(or you can just 'Generate a Test' for this class)
*/
package demo;
public class LeapY ear {
public static boolean isLeapYear(int year) {
if(year < 1){
throw new IllegalArgumentException();
}
if(year % 400 == 0){
return true;
}
if(year % 100 == 0){
return false;
}
if(year % 4 == 0){
return true;
}
return false;
}
}
生成的Junit测试代码:
/**
* Generated by Agitar build: JUnitFactory V ersion 2.2.0.000710 (Build date: Jan 15, 2008) [2.2.0.000710]
* JDK V ersion: 1.6.0_01
* Generated on Mar 14, 2008 2:33:28 AM
* Time to generate: 00:10.863 seconds
*/
package demo;
import com.agitar.lib.junit.AgitarTestCase;
public class LeapY earAgitarTest extends AgitarTestCase {
public Class getTargetClass() {
return LeapY ear.class;
}
public void testConstructor() throws Throwable {
new LeapY ear();
assertTrue("Test call resulted in expected outcome", true);
}
public void testIsLeapY ear() throws Throwable {
boolean result = LeapY ear.isLeapYear(100);
assertFalse("result", result);
}
public void testIsLeapY ear1() throws Throwable {
boolean result = LeapY ear.isLeapYear(1);
assertFalse("result", result);
}
public void testIsLeapY ear2() throws Throwable {
boolean result = LeapY ear.isLeapYear(4);
assertTrue("result", result);
}
public void testIsLeapY ear3() throws Throwable {
boolean result = LeapY ear.isLeapYear(2);
assertFalse("result", result);
}
public void testIsLeapY ear4() throws Throwable {
boolean result = LeapY ear.isLeapYear(10000);
assertTrue("result", result);
}
public void testIsLeapY earThrowsIllegalArgumentException() throws Throwable { try {
LeapY ear.isLeapY ear(0);
fail("Expected IllegalArgumentException to be thrown");
} catch (IllegalArgumentException ex) {
assertNull("ex.getMessage()", ex.getMessage());
assertThrownBy(LeapY ear.class, ex);
}
}
}
Demo地址:/demo/
用户可以到这个Demo地址中输入任何代码来查看生成的Junit TestCase 。

Generate Dashboard可以输出Junit TestCase的运行结果和代码分析报告(可以点击eclipse中JUnit Factory视图中的View Dashboard来打开浏览器显示代码分析报告),
如图2
另外,查看Dashboard时,在浏览器中把index.html换成dashboard.xm能看到很多详细数据!(这个需要查看者熟悉XSLT最好)
如图3
注意:
我有一个想法,因为Junit Factory是可以和ant一起协作的,我觉得可以利用每日构建系统Build System ,把Junit与Build System结合,独立成为一个服务器。

下班时开发人员将当天开发代码提交到SVN的test分支下,由Build System在夜间download SVN服务器上的代码,并且利用Junit Factory生成Junit TestCase,并且执行测试,这样第二天早晨开发人员就可以看到前一天代码的测试结果和分析报告,知道测试用例的Fail数量、Pass数量、覆盖率、代码规范错误、Risk Classes等等指标!不知是否具有实用价值,是否可行!???
Running your tests in Eclipse is good. Running them in your build is even better.
因为学习这个工具时间不长,先写这些,其它功能以后再写!
希望大家一起交流学习!。

相关文档
最新文档