前段时间去亚信面试,被问到写一个冒泡排序,心想这多新鲜,刷刷几下写好。面试官突然问,你怎么对这个程序进行单元测试?
单元测试?!
懵圈...
单元测试
代码是为了什么,当然是重复运行。你的程序主要是由一个个的 Class 组成的,一个类或一个对象当然也是一个单元,而比类更小的单元是类的方法(函式)。如果你的类中的基本单元——如某些方法不能正常工作,在某些输入条件下会得出错误的执行结果,那么如何保证你的类/对象乃至整个应用软件或系统作为一个整体能正常工作呢?所以,简单说,单元测试(优先)的目的就是首先保证一个系统的基本组成单元、模块(如对象以及对象中的方法)能正常工作,这是一种分而治之中的 bottom-up 思想。
百度百科给出的单元测试的定义:http://baike.baidu.com/link?url=tBjOgVVmpid3gjdl4H_7FbqG5LAnjof7FYrL5thzI861kyumRwU8vS6HmSLUvB1pr0sfmj1O3UIJHGHEnlbWKlCPbej_hyKYA6Tqr4ZskhFjoixqsEQpVtNkjxy8awax
工具
对于java代码进行单元测试,我选用的是JUnit(推荐使用JUnit4)。
JUnit 在日常开发中还是很常用的,而且 Java 的各种 IDE (Eclipse、MyEclipse、IntelliJ IDEA)都集成了 JUnit 的组件。当然,自己添加插件也是很方便的。JUnit 框架是 Java 语言单元测试当前的一站式解决方案。这个框架值得称赞,因为它把测试驱动的开发思想介绍给 Java 开发人员并教给他们如何有效地编写单元测试。
断言
断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过。
1)assertEquals()方法
判断两个对象是否相等,并返回boolean,int等类型,前为期望值,后为输入值。
assertEquals(返回值,Object expected,Object actual);
2)assertTrue()/assertFalse()
判断测试的对错,condition是期望,message是实际值。
assertTrue(condition,message);
或assertFalse(condition,message);
3)assertArrayEquals()
判断两个数组是否相等
assertArrayEquals(a[],b[]);
例子:
(1)我于上午编写了一个三角形判定的代码,包含四个小函数,并用assertEquals()与assertTrue()/assertFalse()进行单元测试。
Triangle.java:
public class Triangle {
protected long lborderA = 0;
protected long lborderB = 0;
protected long lborderC = 0;
// Constructor
public Triangle(long lborderA, long lborderB, long lborderC) {
this.lborderA = lborderA;
this.lborderB = lborderB;
this.lborderC = lborderC;
}
/**
* check if it is a triangle
*
* @return true for triangle and false not
*/
public boolean isTriangle(Triangle triangle) {
boolean isTriangle = false;
// check boundary
if ((triangle.lborderA > 0 && triangle.lborderA <= Long.MAX_VALUE)
&& (triangle.lborderB > 0 && triangle.lborderB <= Long.MAX_VALUE)
&& (triangle.lborderC > 0 && triangle.lborderC <= Long.MAX_VALUE)) {
protected long lborderA = 0;
protected long lborderB = 0;
protected long lborderC = 0;
// Constructor
public Triangle(long lborderA, long lborderB, long lborderC) {
this.lborderA = lborderA;
this.lborderB = lborderB;
this.lborderC = lborderC;
}
/**
* check if it is a triangle
*
* @return true for triangle and false not
*/
public boolean isTriangle(Triangle triangle) {
boolean isTriangle = false;
// check boundary
if ((triangle.lborderA > 0 && triangle.lborderA <= Long.MAX_VALUE)
&& (triangle.lborderB > 0 && triangle.lborderB <= Long.MAX_VALUE)
&& (triangle.lborderC > 0 && triangle.lborderC <= Long.MAX_VALUE)) {
// check if subtraction of two border larger than the third
if (diffOfBorders(triangle.lborderA, triangle.lborderB) < triangle.lborderC
&& diffOfBorders(triangle.lborderB, triangle.lborderC) < triangle.lborderA
&& diffOfBorders(triangle.lborderC, triangle.lborderA) < triangle.lborderB) {
isTriangle = true;
}
}
return isTriangle;
}
/**
* Check the type of triangle
*
* Consists of "Illegal", "Regular", "Scalene", "Isosceles"
*/
public String getType(Triangle triangle) {
String strType = "Illegal";
if (isTriangle(triangle)) {
// Is Regular
if (triangle.lborderA == triangle.lborderB
&& triangle.lborderB == triangle.lborderC) {
strType = "Regular";
}
// If scalene
else if ((triangle.lborderA != triangle.lborderB)
&& (triangle.lborderB != triangle.lborderC)
&& (triangle.lborderA != triangle.lborderC)) {
strType = "Scalene";
}
// if isosceles
else {
strType = "Isosceles";
}
}
return strType;
}
/**
* calculate the diff between borders
*
* */
public long diffOfBorders(long a, long b) {
return (a > b) ? (a - b) : (b - a);
}
/**
* get length of borders
*/
public long[] getBorders() {
long[] borders = new long[3];
borders[0] = this.lborderA;
borders[1] = this.lborderB;
borders[2] = this.lborderC;
return borders;
}
}
TriangleTest.java:
import static org.junit.Assert.*;
import org.junit.Test;
public class TriangleTest {
Triangle T1 = new Triangle(2, 3, 4);
Triangle T2 = new Triangle(3, 4, 5);
Triangle T3 = new Triangle(4, 4, 5);
Triangle T4 = new Triangle(-1, 4, 5);
Triangle T5 = new Triangle(4, 4, 4);
Triangle T6 = new Triangle(6, 4, 5);
Triangle T7 = new Triangle(10, 4, 5);
Triangle T8 = new Triangle(6, 4, 5);
@Test
public void testIsTriangle() {
assertEquals(true, T1.isTriangle(T1));
assertEquals("Isosceles", T3.getType(T3));
assertEquals(false, T4.isTriangle(T4));
assertEquals("Regular", T5.getType(T5));
assertEquals("Scalene", T6.getType(T6));
assertEquals(false, T7.isTriangle(T7));
assertEquals(1, T1.diffOfBorders(2, 3));
assertEquals(2, T1.getBorders()[0]);
assertEquals("Illegal", T4.getType(T4));
}
}
(2)于下午14点编写了一个排序算法,并用assertArrayEquals进行单元测试
Sorting.java:
import java.util.Comparator;
import java.util.Random;
/**
* A class that contains several sorting routines,
* implemented as static methods.
* Arrays are rearranged with smallest item first,
* using compareTo.
* @author Mark Allen Weiss
*/
public final class Sorting
{
/**
* Simple insertion sort.
* @param a an array of Comparable items.
*/
public void insertionSort( int[ ] a )
{
int j;
for( int p = 1; p < a.length; p++ )
{
int tmp = a[ p ];
for( j = p; j > 0 && tmp<a[ j - 1 ]; j-- )
a[ j ] = a[ j - 1 ];
a[ j ] = tmp;
}
}
public boolean isSorted(int[] a) {
for(int i=0; i<a.length-1; i++) {
if(a[i]>a[i+1]) {
return false;
}
}
return true;
}
//line74
public static void quicksort( int[ ] a )
{
quicksort( a, 0, a.length - 1 );
}
private static final int CUTOFF = 10;
public static final void swapReferences( Object [ ] a, int index1, int index2 )
{
Object tmp = a[ index1 ];
a[ index1 ] = a[ index2 ];
a[ index2 ] = tmp;
}
public static final void swap(int[] a,int index1,int index2) {
int tmp = a[ index1 ];
a[ index1 ] = a[ index2 ];
a[ index2 ] = tmp;
}
private static int median3( int[ ] a, int left, int right )
{
int center = ( left + right ) / 2;
if( a[ center ]<a[ left ] )
swap( a, left, center );
if( a[ right ] < a[ left ] )
swap( a, left, right );
if( a[ right ] < a[ center ] )
swap( a, center, right );
// Place pivot at position right - 1
swap( a, center, right - 1 );
return a[ right - 1 ];
}
private static void quicksort( int[ ] a, int left, int right)
{
if( left + CUTOFF <= right )
{
int pivot = median3( a, left, right );
int i = left, j = right - 1;
for( ; ; )
{
while( a[ ++i ] < pivot ) { }
while( a[ --j ] > pivot ) { }
if( i < j )
swap( a, i, j );
else
break;
}
swap( a, i, right - 1 ); // Restore pivot
quicksort( a, left, i - 1 ); // Sort small elements
quicksort( a, i + 1, right ); // Sort large elements
}
else // Do an insertion sort on the subarray
insertionSort( a, left, right );
}
private static void insertionSort( int[ ] a, int left, int right )
{
for( int p = left + 1; p <= right; p++ )
{
int tmp = a[ p ];
int j;
for( j = p; j > left && tmp < a[ j - 1 ]; j-- )
a[ j ] = a[ j - 1 ];
a[ j ] = tmp;
}
}
private static final int NUM_ITEMS = 1000;
private static int theSeed = 1;
}
import java.util.Random;
/**
* A class that contains several sorting routines,
* implemented as static methods.
* Arrays are rearranged with smallest item first,
* using compareTo.
* @author Mark Allen Weiss
*/
public final class Sorting
{
/**
* Simple insertion sort.
* @param a an array of Comparable items.
*/
public void insertionSort( int[ ] a )
{
int j;
for( int p = 1; p < a.length; p++ )
{
int tmp = a[ p ];
for( j = p; j > 0 && tmp<a[ j - 1 ]; j-- )
a[ j ] = a[ j - 1 ];
a[ j ] = tmp;
}
}
public boolean isSorted(int[] a) {
for(int i=0; i<a.length-1; i++) {
if(a[i]>a[i+1]) {
return false;
}
}
return true;
}
//line74
public static void quicksort( int[ ] a )
{
quicksort( a, 0, a.length - 1 );
}
private static final int CUTOFF = 10;
public static final void swapReferences( Object [ ] a, int index1, int index2 )
{
Object tmp = a[ index1 ];
a[ index1 ] = a[ index2 ];
a[ index2 ] = tmp;
}
public static final void swap(int[] a,int index1,int index2) {
int tmp = a[ index1 ];
a[ index1 ] = a[ index2 ];
a[ index2 ] = tmp;
}
private static int median3( int[ ] a, int left, int right )
{
int center = ( left + right ) / 2;
if( a[ center ]<a[ left ] )
swap( a, left, center );
if( a[ right ] < a[ left ] )
swap( a, left, right );
if( a[ right ] < a[ center ] )
swap( a, center, right );
// Place pivot at position right - 1
swap( a, center, right - 1 );
return a[ right - 1 ];
}
private static void quicksort( int[ ] a, int left, int right)
{
if( left + CUTOFF <= right )
{
int pivot = median3( a, left, right );
int i = left, j = right - 1;
for( ; ; )
{
while( a[ ++i ] < pivot ) { }
while( a[ --j ] > pivot ) { }
if( i < j )
swap( a, i, j );
else
break;
}
swap( a, i, right - 1 ); // Restore pivot
quicksort( a, left, i - 1 ); // Sort small elements
quicksort( a, i + 1, right ); // Sort large elements
}
else // Do an insertion sort on the subarray
insertionSort( a, left, right );
}
private static void insertionSort( int[ ] a, int left, int right )
{
for( int p = left + 1; p <= right; p++ )
{
int tmp = a[ p ];
int j;
for( j = p; j > left && tmp < a[ j - 1 ]; j-- )
a[ j ] = a[ j - 1 ];
a[ j ] = tmp;
}
}
private static final int NUM_ITEMS = 1000;
private static int theSeed = 1;
}
SortingTest1.java:
import static org.junit.Assert.*;
import org.junit.Test;
public class SortingTest1 {
Sorting s1=new Sorting();
@Test
public void testInsertionSort() {
int a[]={2,3,1,5,3,11};
int b[]={1,2,3,3,11,5};
s1.insertionSort(a);
assertArrayEquals(b,a);
}
@Test
public void testIsSorted() {
int c[]={3,2,2,4,2};
int d[]={1,2,2,4,5};
assertFalse(s1.isSorted(c));
assertTrue(s1.isSorted(d));
}
@Test
public void testQuicksort() {
int e[]={2,3,7,5,4,7,0};
int f[]={0,2,3,4,5,7,7};
s1.quicksort(e);
assertArrayEquals(e,f);
int j[]={7,9,8,0,6,5,4,2,3,1,7};
int k[]={0,1,2,3,4,5,6,7,7,8,9};
s1.quicksort(j);
assertArrayEquals(k,j);
}
@Test
public void testSwapReferences() {
Sorting s4=new Sorting();
Object m[]={s4.equals(s4),s4.getClass(),s4.hashCode()};
Object n[]={s4.hashCode(),s4.getClass(),s4.equals(s4)};
s4.swapReferences(m, 0, 2);
assertArrayEquals(n,m);
}
@Test
public void testSwap() {
int y[]={4,2,0};
int z[]={0,2,4};
s1.swap(y, 0, 2);
assertArrayEquals(z,y);
}
}
import org.junit.Test;
public class SortingTest1 {
Sorting s1=new Sorting();
@Test
public void testInsertionSort() {
int a[]={2,3,1,5,3,11};
int b[]={1,2,3,3,11,5};
s1.insertionSort(a);
assertArrayEquals(b,a);
}
@Test
public void testIsSorted() {
int c[]={3,2,2,4,2};
int d[]={1,2,2,4,5};
assertFalse(s1.isSorted(c));
assertTrue(s1.isSorted(d));
}
@Test
public void testQuicksort() {
int e[]={2,3,7,5,4,7,0};
int f[]={0,2,3,4,5,7,7};
s1.quicksort(e);
assertArrayEquals(e,f);
int j[]={7,9,8,0,6,5,4,2,3,1,7};
int k[]={0,1,2,3,4,5,6,7,7,8,9};
s1.quicksort(j);
assertArrayEquals(k,j);
}
@Test
public void testSwapReferences() {
Sorting s4=new Sorting();
Object m[]={s4.equals(s4),s4.getClass(),s4.hashCode()};
Object n[]={s4.hashCode(),s4.getClass(),s4.equals(s4)};
s4.swapReferences(m, 0, 2);
assertArrayEquals(n,m);
}
@Test
public void testSwap() {
int y[]={4,2,0};
int z[]={0,2,4};
s1.swap(y, 0, 2);
assertArrayEquals(z,y);
}
}