Junit
单元测试
今天讲解了Junit框架,对单元测试有了解。原来也用过Junit,感觉和程序的Main函数没什么两样,但是今天学习以后感觉Junit的功能比Main函数强大的多,而且2者根本没法比。在一个中大型项目中,单元测试是必不可少的,通过测试可以保证工程软件的健壮性,而且在以后的模块修改中可以很容易测试出修改后的bug。但是怎么测试呢?通过Junit编写一些好的测试用列就能实现黑合测试。下面我们来讲述一个简单的Junit测试用列。(也可以参考junit文件包里的 junit/samples/AllTest.java)
首先把设置classpath=”……..junit/junit.jar”
然后编写测试用列(注意测试用列的类必须继承一个 TestCase)
public class TestConnection extends TestCase{
private ConnectionPool pool = null;
public void setUp()
{
pool = new ConnectionPool();
}
/**
* 测试ConnectionPool的init方法
*
*/
public void testInit()
{
try {
pool.init();
} catch (ClassNotFoundException e) {
Assert.fail();
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
Assert.fail();
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试ConnectionPool的getconection方法
*
*/
public void testGetConnection()
{
Connection a = pool.getConnection();
Assert.assertNotNull(a);
}
public void testReadProperties()
{
try {
pool.readProperties();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Assert.fail();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Assert.fail();
e.printStackTrace();
}
}
public void tearDown()
{
}
}
其中的setUp()和tearDown()分别是做初始化和结束时候的操作。
以上就是一个最简单的测试用列,由于这是一个简单的测试用列,还有些不规范。这个测试用列一些初学者一定会问,这个和Main方法实现的功能不是差不多啊。为什么要用Junit呢。但是在一个项目里需要对很多类都进行测试,如果都写一个Main函数这样不是很麻烦,而且很不规范。用Junit可以很容易的将几个测试用列加入一个大的测试用列中一起测试。这就要用到一个类TestSuite。
public class MyTestSuite {
//组装测试
public static void main(String args[])
{
junit.textui.TestRunner.run (suite());
}
public static Test suite( )
{
TestSuite suite= new TestSuite("All JUnit Tests");
suite.addTest(new TestSuite(TestConnection.class));
suite.addTest(new TestSuite(TestOperatorDatabase.class));
return suite;
}
}
这样的测试组装可以是一个树形的结构,看到这里大家应该体会到Junit的强大的测试功能了吧,而且编写一个好的测试用列对系统的维护和完整性都有好大的帮助,所以大家要重视测试用列的编写。
下面我们来讲讲
Ant
和
Junit
的集成。
Ant中把Junit做为一个可选任务,所以在Junit中可以进行测试.(注意,因为是一个可选任务,所以要用Junit任务必须把Junit的jar包加入到classpath中,办法有3中,可以查看Ant帮助)
Junit任务很好编写(这个是Ant里的帮助的引用)
<junit printsummary="yes" haltonfailure="yes"> <!—是否打印摘要 à
<!—测试classpath à
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${java.class.path}"/>
</classpath>
<!-- 格式类型 à
<formatter type="plain"/>
<!-- 测试用列的名字 à
<test name="my.test.TestCase" haltonfailure="no" outfile="result">
<formatter type="xml"/>
</test>
<!-- 批处理测试类 à
<batchtest fork="yes" todir="${reports.tests}"><!—todir是输出到哪个目录下 à
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
其中还有很多的元素都很简单,具体见Ant帮助.其中注意的是haltonfailure="yes" 如果设置了等于yes那么如果出现failre就会结束Ant,那么后面的语句将不会执行了.举例
<target name="run" depends="compile">
<mkdir dir="${report.dir}" />
<junit printsummary="yes" fork="No"><!-- 注意,这里如果要打印报告不要设置 haltonfailure="no" 属性 -->
<classpath>
<pathelement location="${class.dir}"/>
<pathelement location="${junit.jar}"/>
<pathelement location="${oracle.jar}"/>
</classpath>
<formatter type="xml"/>
<test name="${classname}.MyTestSuite" todir="./${report.dir}"/>
</junit>
</target>
<target name="echo" >
<echo message="${junit.jar}" />
</target>
<!-- 读生成的JUNIT XML文档
-->
<target name="report" depends="run,echo">
<!--mkdir dir="./maomao/${report.dir}" /-->
<junitreport todir="./${report.dir}">
<fileset dir="./${report.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="./xx/html"/>
</junitreport>
</target>
<!-- END -->
如果设置了haltonfailure="yes" 任务report将不会被执行.
(report任务是读取Junit生成的XML文档)
学习总结
通过今天的学习,我对Junit的测试框架的功能和测试用列等有了一些理解,在以后的工作中如果有机会我会很负责的编写测试用列.因为感觉这个对一个工程的各方面等有很大的联系
.