搭建持續集成單元測試平台(Jenkins+Ant+Java+Junit+SVN)


一、環境准備

  1. Jenkins:
    1. 到官網下載jenkins.war包:http://jenkins-ci.org/
    2. 安裝方法有兩種:
      1. 把下載下來的jenkins.war包放到文件夾下,如C:\jenkins,然后打開命令行窗口並進到該目錄下,執行java -jar jenkens.war命令,當提示:“Jenkins is fully up and running”時,表示啟動成功,這時在瀏覽器窗口輸入:http://localhost:8080/ 就可到jenkins的首頁。
      2. 如果有tomcat,把jenkins.war包放在tomcat的webapps文件夾下,啟動tomcat時會自動啟動jenkins,這時通過http://localhost:8080/jenkins就 可以訪問jenkins的首頁了。
  2. ANT:

      下載ant並配置ANT_HOME,官網:http://ant.apache.org/

  3、Junit:

      下載junit.jar包,沒用過的可參考:http://blog.csdn.net/lengyuhong/article/details/5815017

  4、SVN:

      1、用本地硬盤當SVN倉庫:http://wenku.baidu.com/view/12b02f6a011ca300a6c39081.html

      2、SVN服務器搭建和使用:http://www.cnblogs.com/xiaobaihome/tag/SVN/ (推薦用此種方法,后面有原因

二、項目代碼:

  環境准備好了之后就可開始寫代碼、單元測試案例以及ANT用來構建的build.xml文件,這些內容在上一篇ANT task之Junit、JunitReport有講過,這里不細講:

1、Java代碼:

package com.glen.he;

public class ComplexCalculation {
    public int Division(int a,int b){        
        return (a/b);        
    }

    public int Multiply(int a,int b){        
        return (a*b);        
    }
}
ComplexCalculation.java
package com.glen.he;

public class SimpleCalculation {
    public int Add(int a,int b){        
        return (a+b);        
    }

    public int Subtration(int a,int b){
        return(a-b);
    }
    
}
SimpleCalculation.java

2、單元測試代碼:  

package com.glen.he;

import com.glen.he.ComplexCalculation;

import static org.junit.Assert.*;

import org.junit.Test;


public class ComplexCalculationTest {
    
    ComplexCalculation cc = new ComplexCalculation();
    
    @Test
    public void DivisionTest() {
        
        int c = cc.Division(100, 5);
        
        assertEquals(20, c);        
    }

    @Test
    public void MultiplyTest() {
        
        int c = cc.Multiply(100, 5);
        
        assertEquals(500, c);        
    }
}
ComplexCalculationTest.java
package com.glen.he;

import com.glen.he.SimpleCalculation;

import static org.junit.Assert.*;
import org.junit.Test;

public class SimpleCalculationTest {

    SimpleCalculation sc = new SimpleCalculation();
    
    @Test
    public void AddTest() {
        
        int c = sc.Add(3, 5);    
        
        assertEquals(8, c);        
    }
    
    @Test
    public void SubtrationTest() {
        
        int c = sc.Subtration(20, 5);    
        
        assertEquals(15, c);        
    }
}
SimpleCalculationTest.java

3、build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="AntDemo" default="junit" basedir=".">
    <!-- =================================================================== -->
    <!-- 變量設置  -->
    <!-- =================================================================== -->

    <!-- 源代碼src路徑 -->
    <property name="src.path" value="src/java"/>
    <!-- 單元測試代碼路徑 -->
    <property name="test.path" value="src/test"/>
    <!-- 編譯文件class路徑 -->
    <property name="build.path" value="build"/>
    <!-- jar包路徑 -->
    <property name="dist.path" value="dist"/>
    <!-- lib包路徑 -->
    <property name="lib.path" value="lib"/>
    <!-- 生成報告junit4.xml路徑 -->
    <property name="report.path" value="report"/>
        
    <!-- =================================================================== -->
    <!-- 設置classpath -->
    <!-- =================================================================== -->
    <path id="compile.path">        
        <fileset dir="${lib.path}">
            <include name="**/*.jar"/>
        </fileset>
        
        <pathelement path="${build.path}"/>
    </path>     

    <!-- 初始化 -->
    <target name="init">        
        <mkdir dir="${build.path}"/>
        <mkdir dir="${report.path}"/>
        <mkdir dir="${dist.path}"/>
    </target>
    
    <!-- =================================================================== -->
    <!-- 清除歷史編譯class -->
    <!-- =================================================================== -->
    <target name="clean" description="clean">        
        <delete dir="${build.path}"/>
        <delete dir="${report.path}"/>
        <delete dir="${dist.path}"/>
    </target>

    <!-- =================================================================== -->
    <!-- 編譯測試文件,初始化目錄 -->
    <!-- =================================================================== -->
    <target name="compile" depends="init">
        <javac srcdir="${src.path}" destdir="${build.path}"  classpathref="compile.path" includeantruntime="true"/>
        <javac srcdir="${test.path}" destdir="${build.path}"  classpathref="compile.path" includeantruntime="true"/>
    </target>      
         
    <!-- =================================================================== -->
    <!-- 執行測試案例 -->
    <!-- =================================================================== -->
    <target name="junit" depends="compile">                
        <junit printsummary="true" fork="true">        
             <formatter type="xml" usefile="true"/>        
            
             <classpath refid="compile.path"/>        
            
            <batchtest fork="on" todir="${report.path}" haltonfailure="no">
                <fileset dir="${build.path}">
                    <include name="**/*Test.class"/>
                </fileset>
            </batchtest>                 
         </junit>        
     </target>
    
    <target name="junit-report" depends="junit">        
        <!-- 產生單元測試報表文檔 -->
        <junitreport todir="${report.path}">
            <fileset dir="${report.path}">
                <include name="TEST-*.xml" />
            </fileset>
            
            <report format="frames" todir="${report.path}" />
        </junitreport>
    </target>

    <target name="make-jar" depends="compile" description="make jar file">
          <jar jarfile="${dist.path}/AntDemo.jar">
               <fileset dir="${build.path}">

                <!--除去test文件-->
                <exclude name="**/*Test.class"/>
               </fileset>
          </jar>
     </target>    
    
</project>
build.xml

 

三、配置Jenkins:

  PS:Jenkins可以通過master/slave來支持分布式的job運行,本文運行在master,即Jenkins所在的機器。

  1、打開jenkins首頁,新建一個job,輸入Item名稱,選擇 構建一個自由風格的軟件項目,點擊"OK"  

 

  2、在 源碼管理 那里,選擇Subversion,在Repository URL后面,輸入你的SVN地址。  

      PS:Repository URL使用本地磁盤當倉庫這種方法后來我在其它機器上試驗時,發現老是報錯:svn: E180001: Unable to open an ra_local session to URL。一時沒有找到解決辦法,大家如果也碰到此問題,可以搭建SVN服務器來管理源代碼,我試了,挺好使的。

 

  3、在 構建 那里也可以有兩種做法:

   I、選擇Execute Windows batch command,在輸入框輸入如下命令(這里我選擇的是這個方法):

      set path=C:\ANT_HOME\Apache-Ant-1.7.0\bin;path  把ant的安裝目錄添加到path

      ant junit                        執行junit task      

   II、方法I比較麻煩,如果我們設置好了ANT_HOME,可以選擇Invoke Ant,然后在targets里面指定我們build.xml里的task name。       

  4、點擊保存,然后選擇立即構建,執行結果:  

 

 

 

 

參考資料:

http://hi.baidu.com/janice515/item/3272fe9b99eb4cc8b6253101 

http://blog.csdn.net/lengyuhong/article/details/5828770

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM