路人

导航

<2006年3月>
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿

随笔档案(39)

相册

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

junit+ant

今天得朱老师指点~试了一下junit+ant~

教程来源:
1,Eclipse中使用Junit插件测试
http://www.yzcc.com/yzcc/java/135236586.htm#
2,Eclipse快速上手指南之使用ANT
http://www.yzcc.com/yzcc/java/090853462_2.htm

注意build.xml代码如果是黑白的~那么手动把<>括号改一下(英文输入法状态下)~~

还需要注意一下path和java_home,如果不能生成doc,可能是你的目录有问题~~~

package com.ant;
//一般代码
public class Hello {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub

    }


    
/** this is zhushi! */
    
public static int abs(int n) {
        System.out.println(
"success!");
        
return n >= 0 ? n : (-n);
    }

}

//junit代码

package com.ant;

import junit.framework.TestCase;

public class HelloTest extends TestCase {
    
private Hello hello;

    
protected void setUp() throws Exception {
        
super.setUp();
        hello 
= new Hello();
    }


    
protected void tearDown() throws Exception {
        
super.tearDown();
    }


    
/*
     * Test method for 'com.ant.Hello.abs(int)'
     
*/

    
public void testAbs() {
        assertEquals(hello.abs(
14), 14);
        assertEquals(hello.abs(
-5), 5);
        assertEquals(hello.abs(
0), 0);
    }


}

<?xml version="1.0"?>

<!--build.xml-->

<project name="Hello world" default="doc">
    
<!-- properies -->
    
<property name="src.dir" value="src" />
    
<property name="report.dir" value="report" />
    
<property name="classes.dir" value="classes" />
    
<property name="lib.dir" value="lib" />
    
<property name="dist.dir" value="dist" />
    
<property name="doc.dir" value="doc" />

    
<!-- 定义classpath -->
    
<path id="master-classpath">
        
<fileset file="${lib.dir}/*.jar" />
        
<pathelement path="${classes.dir}" />
    
</path>

    
<!-- 初始化任务 -->
    
<target name="init">
    
</target>

    
<!-- 编译 -->
    
<target name="compile" depends="init" description="compile the source files">
        
<mkdir dir="${classes.dir}" />
        
<javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.4">
            
<classpath refid="master-classpath" />
        
</javac>
    
</target>

    
<!-- 测试 -->
    
<target name="test" depends="compile" description="run junit test">
        
<mkdir dir="${report.dir}" />
        
<junit printsummary="on" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
            
<classpath refid="master-classpath" />
            
<formatter type="plain" />
            
<batchtest todir="${report.dir}">
                
<fileset dir="${classes.dir}">
                    
<include name="**/*Test.*" />
                
</fileset>
            
</batchtest>
        
</junit>
        
<fail if="tests.failed">
***********************************************************
**** One or more tests failed! Check the output  ****
***********************************************************
</fail>
    
</target>

    
<!-- 打包成jar -->
    
<target name="pack" depends="test" description="make .jar file">
        
<mkdir dir="${dist.dir}" />
        
<jar destfile="${dist.dir}/hello.jar" basedir="${classes.dir}">
            
<exclude name="**/*Test.*" />
            
<exclude name="**/Test*.*" />
        
</jar>
    
</target>

    
<!-- 输出api文档 -->
    
<target name="doc" depends="pack" description="create api doc">
        
<mkdir dir="${doc.dir}" />
        
<javadoc destdir="${doc.dir}" author="true" version="true" use="true" windowtitle="Test API">
            
<packageset dir="${src.dir}" defaultexcludes="yes">
                
<include name="com/ant/**" />
            
</packageset>
            
<doctitle>
                
<![CDATA[<h1>Hello, test</h1>]]></doctitle>
        
<bottom>
            
<![CDATA[<i>All Rights Reserved.</i>]]>
    
</bottom>
    
<tag name="todo" scope="all" description="To do:" />
</javadoc>
</target>
</project>

posted on 2006-03-19 03:16 山岗 阅读(442) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。