1.极限编程(xp):其实质就是就是基于测试的编程开发,强调最快的开发效率
2.junit3.8开发
Junit3的版本要继承TestCase类
注意:在使用junit进行方法测试时,一定要在方法名前加上test,否则会出现错误。
3.由例子开始我们的单元测试学习
1.首先加入junit支持,即加入其jar文件。
2.在创建source folder,其也会创建到classpath的路径中去
3.编写被测试类hello.java
package com.xk.juint;
public class Hello {
public String hi() {
return "world";
}
public String world() {
return "hi";
}
public String nil() {
return null;
}
public String notNil() {
return "abc";
}
public void test() {
throw new NumberFormatException();
}
}
4.编写测试类
package com.xk.junit;
import com.xk.juint.Hello;
import junit.framework.TestCase;
public class TestHello extends TestCase{
private Hello hello ;
@Override
protected void setUp() throws Exception {
super.setUp();
hello = new Hello();
System.out.println("hello init");
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
System.out.println("hello destroy");
hello = null;
}
public void testHi() {
String str = hello.hi();
assertEquals("测试hi失败", str, "world");
}
public void testWorld() {
String string = hello.world();
assertEquals("测试world失败", string, "hi");
}
public void testNil() {
assertNull("对象不为空", hello.nil());
}
public void testNotNil() {
assertNotNull("对象为空",hello.notNil());
}
public void testTest() {
try {
hello.test();
fail("没有抛出异常");
} catch (NumberFormatException e) {
}
}
}
二.基于junit4的测试
注:可使用静态导入 import static,就可直接使用该导入类的静态方法了
package com.xk.test;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.xk.juint.Hello;
public class TestHello {
private Hello hello;
@Before
public void setUp() {
hello = new Hello();
System.out.println("hello init");
}
@After
public void tearDown() {
hello = null;
System.out.println("hello destroy");
}
@Test
public void testHi() {
String string = hello.hi();
assertEquals("测试hi失败", string, "world");
}
@Test
public void testWorld() {
String string = hello.world();
assertEquals("测试world失败", string, "hi");
}
@Test
public void testNil() {
assertNull("返回不为空",hello.nil());
}
@Test
public void testNotNil() {
assertNotNull("返回为空",hello.notNil());
}
@Test(expected=NumberFormatException.class)
public void testtest() {
hello.test();
}
}
三.Ant 与junit的整合(使用ant进行junit的单元测试)(非常重要)
1.创建属性
2.编译源文件
3.编译test的文件
4.运行单元测试
5.生成单元测试的报告
注:最佳实践添加path,采用链式添加
下例重要的build.xml(***)
<?xml version="1.0" encoding="UTF-8"?>
<project>
<property name="src.dir" location="src"></property>
<property name="test.src.dir" location="test"></property>
<property name="build.dir" location="build"></property>
<property name="build.classes" location="${build.dir}/classes"></property>
<property name="build.test.dir" location="${build.dir}/test"></property>
<property name="build.test.classes" location="${build.test.dir}/classes"></property>
<property name="build.test.report" location="${build.test.dir}/report"></property>
<property name="lib.dir" location="lib"></property>
<property name="run.test.class" value="**/Test*"></property>
<path id="compile-path">
<fileset dir="${lib.dir}" includes="*.jar"></fileset>
</path>
<path id="compile-test-path">
<path refid="compile-path"></path>
<pathelement location="${build.classes}"/>
</path>
<path id="run-test-path">
<path refid="compile-test-path"></path>
<pathelement location="${build.test.classes}"/>
</path>
<target name="clear">
<echo>进行清理操作</echo>
<delete dir="${build.dir}"></delete>
</target>
<target name="init">
<echo>进行初始化操作</echo>
<mkdir dir="${build.dir}"></mkdir>
<mkdir dir="${build.classes}"></mkdir>
<mkdir dir="${build.test.dir}"></mkdir>
<mkdir dir="${build.test.classes}"></mkdir>
<mkdir dir="${build.test.report}"></mkdir>
</target>
<target name="compile" depends="init">
<echo>编译源文件</echo>
<javac failοnerrοr="true" includeantruntime="true" srcdir="${src.dir}" destdir="${build.classes}" classpathref="compile-path"></javac>
</target>
<target name="compile.test" depends="compile">
<echo>编译测试源文件</echo>
<javac failοnerrοr="true" includeantruntime="true" srcdir="${test.src.dir}" destdir="${build.test.classes}" classpathref="compile-test-path"></javac>
</target>
<target name="run-test" depends="compile.test">
<echo>运用单元测试</echo>
<junit printsummary="true" haltonfailure="true">
<classpath refid="run-test-path"></classpath>
<formatter type="brief" usefile="false"/>
<!-- <test name="${run.test.class}" ></test> -->
<formatter type="xml"/>
<batchtest todir="${build.test.report}">
<fileset dir="${build.test.classes}" includes="${run.test.class}"></fileset>
</batchtest>
</junit>
<junitreport todir="${build.test.report}">
<fileset dir="${build.test.report}" includes="Test-*.xml"></fileset>
<report format="frames" todir="${build.test.report}/html"></report>
</junitreport>
</target>
<target name="over" depends="run-test">
<echo>整个过程结束了</echo>
</target>
</project>