代码之美《美丽测试》及JUNIT

针对《美丽测试》中引入的JUNIT,下载了一个JUNIT 4.5试用了一下:
public class Main {
   /**
    * 二分查找(非迭代)
    *    
    * @return
    */
   public static boolean binaryQuery( int[] test, int target) {
     int low = 0;
     int high = test.length - 1;
     while (low <= high) {
       int mid = getMidValue(low, high);
       int midValue = test[mid];
       if (mid == low || mid == high) {
         return midValue == target;
      }
       if (midValue < target) {
        low = mid;
      } else if (midValue > target) {
        high = mid;
      } else {
         return true;
      }
    }
     return false;
  }

   static int getMidValue( int low, int high) {
     return high + low >>> 1;
  }
}
 
下面是测试用例:
package jtest;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class QueryTest extends TestCase {
   /**
    * 冒烟测试
    */
   public void testQuery() {
     int test[] = new int[] { 1, 4, 42, 55, 55, 67, 87, 100, 245 };
    assertEquals( true, Main.binaryQuery(test, 55));
    assertEquals( false, Main.binaryQuery(test, 45));
  }

   /**
    * 测试数组为空的情况
    */
   public void testEmpty() {
     int test[] = new int[] {};
    assertEquals( false, Main.binaryQuery(test, 12));
  }

   /**
    * 测试中间值的生成
    */
   public void testMidValue() {
    assertEquals(0, Main.getMidValue(0, 1));
    assertEquals(3, Main.getMidValue(0, 6));
    assertEquals(12000000, Main.getMidValue(11000000, 13000000));
    assertEquals(Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2, Integer.MAX_VALUE);
  }

   public static Test suite() {
     return new TestSuite(QueryTest. class);
  }

   /**
    * 运行测试用例
    *    
    * @param args
    */
   public static void main(String[] args) {
    junit.textui.TestRunner.run(suite());
  }
}
测试用例的输出如下:
...
Time: 0.032

OK (3 tests)
 
以ANT运行junit并且生成HTML报告:
< project name ="junit" default ="test-html" basedir ="." >
   < property name ="src" value ="../src" />
   < property name ="lib" value ="../lib" />
   < property name ="classes" value ="../bin" />
   < property name ="test.reports" value ="./report" />
   < property name ="test.class.name" value ="jtest.QueryTest" />
   < path id ="test.classpath" >
     < pathelement location ="${classes}" />
     < fileset dir ="${lib}" >
       < include name ="**/*.jar" />
     </ fileset >
   </ path >
   < target name ="test" >
     < junit fork ="yes" haltonfailure ="yes" >
       < test name ="${test.class.name}" />
       < formatter type ="plain" usefile ="false" />
       < classpath refid ="test.classpath" />
     </ junit >
   </ target >
   < target name ="test-html" >
     < junit fork ="yes" printsummary ="no" haltonfailure ="no" >
       < batchtest fork ="yes" todir ="${test.reports}" >
         < fileset dir ="${classes}" >
          <!-- not all classes    
          <include name="**/*Test.class" />
          -->
           < include name ="**/QueryTest.class" />
         </ fileset >
       </ batchtest >
       < formatter type ="xml" />
       < classpath refid ="test.classpath" />
     </ junit >
     < junitreport todir ="${test.reports}" >
       < fileset dir ="${test.reports}" >
         < include name ="TEST-*.xml" />
       </ fileset >
       < report todir ="${test.reports}" />
     </ junitreport >
   </ target >
</ project >
运行test任务有如下的输出:
Buildfile: F:\dev\eclipse\workspace\pack\ant\build.xml
test:
        [junit] Testsuite: jtest.QueryTest
        [junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.031 sec
        [junit] Testcase: testQuery took 0 sec
        [junit] Testcase: testEmpty took 0 sec
        [junit] Testcase: testMidValue took 0 sec
BUILD SUCCESSFUL
Total time: 1 second
运行test-html的输出结果如下:
Buildfile: F:\dev\eclipse\workspace\pack\ant\build.xml
test-html:
[junitreport] Processing F:\dev\eclipse\workspace\pack\ant\report\TESTS-TestSuites.xml to C:\DOCUME~1\kinkding\LOCALS~1\Temp\null1206303433
[junitreport] Loading stylesheet jar:file:/F:/dev/eclipse/plugins/org.apache.ant_1.7.0.v200803061910/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
总的文件有:
all-tests.html                         allclasses-frame.html
alltests-errors.html             alltests-fails.html
index.html                                 [jtest]
overview-frame.html                overview-summary.html
stylesheet.css                         TEST-jtest.QueryTest.xml
TESTS-TestSuites.xml

你可能感兴趣的:(职场,休闲)