Junit是JAVA开发中应用最多的单元测试框架,公司内部也逐渐普及到各个项目开发中,虽然Junit测试框架易学易用,但是还是在保证代码质量的同时增加了开发人员的代码工作量,需要拿出部分时间编写Junit测试代码。Junit Factory就能更好的提高单元测试效率!
原理:
Junit Factory和agitarone的一项关键技术就是software agitation,它可以为你自动创建各种测试用例(实际测试必须要测试每一行、每一条分支、甚至每一种输出),而且自动创建的过程是通过对代码的agitation,(一种基于java二进制代码的分析,可以自动得出需要测试的东东)后跟开发者交互来完成的。agitation的这种技术是基于服务器并且是分布式的,可以远程使用,并可以利用多个服务器来完成。
![自动化单元测试工具Junit Factory使用介绍 junit自动化测试](http://img.413yy.cn/images/31101031/31110153t01d5e9100a7e6b8094.jpg)
安装:
Junit Factory是可以作为eclipse插件使用的,也可以和ant一起使用。部门内主要开发工具是eclipse,所以介绍如何在eclipse下用Junit Factory生成Junit单元测试用例!
Download :http://www.junitfactory.com/update/.
Eclipse, 选Help ,选 Software Updates ,点 Find and Install ;
选择“Search For New Features to Install” 然后“Next”;
3. 选New Remote Site ;
4. 输入一个JUnit Factory网站的注册用户名 ;
5. 输入url: http://www.junitfactory.com/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. Your 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 LeapYear {
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 Version 2.2.0.000710 (Build date: Jan 15, 2008) [2.2.0.000710]
* JDK Version: 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 LeapYearAgitarTest extends AgitarTestCase {
public Class getTargetClass() {
return LeapYear.class;
}
public void testConstructor() throws Throwable {
new LeapYear();
assertTrue("Test call resulted in expected outcome", true);
}
public void testIsLeapYear() throws Throwable {
boolean result = LeapYear.isLeapYear(100);
assertFalse("result", result);
}
public void testIsLeapYear1() throws Throwable {
boolean result = LeapYear.isLeapYear(1);
assertFalse("result", result);
}
public void testIsLeapYear2() throws Throwable {
boolean result = LeapYear.isLeapYear(4);
assertTrue("result", result);
}
public void testIsLeapYear3() throws Throwable {
boolean result = LeapYear.isLeapYear(2);
assertFalse("result", result);
}
public void testIsLeapYear4() throws Throwable {
boolean result = LeapYear.isLeapYear(10000);
assertTrue("result", result);
}
public void testIsLeapYearThrowsIllegalArgumentException() throws Throwable {
try {
LeapYear.isLeapYear(0);
fail("Expected IllegalArgumentException to be thrown");
} catch (IllegalArgumentException ex) {
assertNull("ex.getMessage()", ex.getMessage());
assertThrownBy(LeapYear.class, ex);
}
}
}
Demo地址:http://www.junitfactory.com/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.
因为学习这个工具时间不长,先写这些,其它功能以后再写!
希望大家一起交流学习!
谨以此文献给工作在开发测试一线的战友们!!!