<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sky's blog

    我和我追逐的夢

    常用鏈接

    統計

    其他鏈接

    友情鏈接

    最新評論

    你走你的陽光道,我走我的獨木橋:整合ant ivy 和testng


        近期自己折騰自己,放著正統的maven + junit不用,卻準備用ant + ivy 替代maven做依賴管理,用testng替代junit做單元測試。

        現在要做的工作,其實很簡單,就是ant的腳本中,搞定相關的target: 編譯,運行單元測試。

        需要的步驟大體如下:

    1. ivy 做依賴解析,得到所有依賴的jar包,以便生成編譯源碼需要的classpath路徑
    這里很重要的一點,是需要區分開編譯正常代碼的classpath和編譯測試代碼的classpath,因為通常情況下testcase需要一些特殊的依賴如juni,testng之類的測試框架,easymock,jmock之類的mock工具。

    2. 編譯代碼和測試案例

    3. 運行testng 來執行testcase

        分別來看三者的實現。

    1) ivy解析依賴

    ant代碼如下:

        <target name="ivy.resolve" description="--> resolve project dependencies by ivy">
            
    <echo>resolve dependencies of project "${ant.project.name}" by ivy</echo>
            
    <ivy:resolve />

            
    <ivy:cachefileset setid="ivy.cachefileset.compile" type="jar,bundle" conf="compile" />

            
    <ivy:cachefileset setid="ivy.cachefileset.test" type="jar,bundle" conf="test" />
        
    </target>

    用到了ivy標準的resolve 任務,然后再用cachefileset來獲取需要的文件列表,以備后面使用。注意type要寫成"jar,bundle",因此目前的依賴包雖然擴張名都是jar,但是它的ivy類型定義確有可能為bundle(都是OSGI惹得禍)。還有上面用了兩次cachefileset任務,conf不同。


    2) 編譯代碼和測試案例

    <target name="compile.compile" depends="ivy.resolve" >
            
    <echo>compile project ${ant.project.name} </echo>
            
    <compile_source_main />
            
    <compile_source_test />
        
    </target>

        
    <macrodef name="compile_source_main">
            
    <sequential>
                
    <echo>compile java classes in project ${ant.project.name} </echo>
                
    <echo>classpath is : ${project.classpath.compile.ivy.lib}</echo>
                
    <delete dir="${dir.target.bin.main}" />
                
    <mkdir dir="${dir.target.bin.main}" />
                
    <javac debug="true" srcdir="${dir.src.main.java}" destdir="${dir.target.bin.main}" target="1.5" includeAntRuntime="false">
                    
    <classpath>
                        
    <pathelement location="${dir.src.main.resources}" />
                        
    <fileset refid="ivy.cachefileset.compile" />
                    
    </classpath>
                
    </javac>
            
    </sequential>
        
    </macrodef>

        
    <macrodef name="compile_source_test">
            
    <sequential>
                
    <echo>compile java testcase in project ${ant.project.name} </echo>
                
    <echo>classpath is : ${project.classpath.test.ivy.lib}</echo>
                
    <delete dir="${dir.target.bin.test}" />
                
    <mkdir dir="${dir.target.bin.test}" />
                
    <javac  debug="true" srcdir="${dir.src.test.java}" destdir="${dir.target.bin.test}" target="1.5" includeAntRuntime="false">
                    
    <classpath>
                        
    <pathelement location="${dir.src.test.resources}" />
                        
    <pathelement location="${dir.src.main.resources}" />
                        
    <pathelement location="${dir.target.bin.main}" />
                        
    <fileset refid="ivy.cachefileset.test" />
                    
    </classpath>
                
    </javac>
            
    </sequential>
        
    </macrodef>


    注意這里用到classpath時,是在上面得到的ivy cachefileset的基礎上,增加其他路徑才得到最終的classpath。曾經在這里折騰了不少時間,因為開始是用ivy的cacheclasspath任務直接拿到一個classpath,然后在這里發現單有這個classpath是不夠的。可是又沒有找到如何從一個classpath生成一個更多內容的classpath的方法(郁悶,ant里面的classpath似乎不支持這種classpath=***+***+classpath的算法,或者是我笨沒有找到)。最后只好改用cachefileset來獲取fileset,然后自己增加其他路徑。典型如編譯測試案例時,必須將前面編譯好的class作為classpath的一部分增加。從這種角度講,ivy的cacheclasspath任務是用處不大的,實用的是cachefileset任務。

    3) 運行testng

    首先需要初始化testng,引入testng的任務。

        <target name="testng.init" depends="ivy.resolve">
            
    <taskdef resource="testngtasks">
                
    <classpath>
                    
    <fileset refid="ivy.cachefileset.test" />
                
    </classpath>
            
    </taskdef>
        
    </target>

    在具體執行testng時,有兩種選擇:

    1. 通過testng.xml指定具體的測試案例

    應該說testng對此有非常強大而富有彈性的支持,通過testng.xml可以指定不同的package,class,可以指定exclude,可以分組,還有其他高級特性。

    2. 運行所有案例
    使用testng.xml文件的前提是項目有提供testng.xml文件,對于一些簡單的項目,可能只是簡單的希望執行所有testcase,因此就需要在運行檢測testng.xml文件存在與否。

        <target name="testng.test" depends="testng.init">
            
    <if>
                
    <resourceexists>
                    
    <file file="${dir.src.test.java}/testng.xml" />
                
    </resourceexists>
                
    <then>
                    
    <run_testng_with_xml />
                
    </then>
                
    <else>
                    
    <run_testng_without_xml />
                
    </else>
            
    </if>
        
    </target>

    testng.xml存在時,通過xmlfileset來調用testng任務:

        <macrodef name="run_testng_with_xml">
            
    <sequential>
                
    <echo>run testng to test project "${ant.project.name}".</echo>
                
    <echo>found ${dir.src.test.java}/testng.xml, use it to run testng.</echo>
                
    <delete dir="${dir.target.testng.testoutput}" />
                
    <testng outputDir="${dir.target.testng.testoutput}" haltOnfailure="true">
                    
    <xmlfileset dir="${dir.src.test.java}" includes="testng.xml" />
                    
    <classpath>
                        
    <pathelement location="${dir.src.test.resources}" />
                        
    <pathelement location="${dir.src.main.resources}" />
                        
    <pathelement location="${dir.target.bin.test}" />
                        
    <pathelement location="${dir.target.bin.main}" />
                        
    <fileset refid="ivy.cachefileset.test" />
                    
    </classpath>
                
    </testng>
            
    </sequential>
        
    </macrodef>

    testng.xml不存在時,通過classfileset來指定需要執行的class:

        <macrodef name="run_testng_without_xml">
            
    <sequential>
                
    <if>
                    
    <resourcecount when="greater" count="0">
                        
    <fileset dir="${dir.target.bin.test}" includes="**/*.class" />
                    
    </resourcecount>
                    
    <then>
                        
    <echo>run testng to test project "${ant.project.name}".</echo>
                        
    <echo>${dir.src.test.java}/testng.xml not found, default to run all the testcase.</echo>
                        
    <delete dir="${dir.target.testng.testoutput}" />
                        
    <testng outputDir="${dir.target.testng.testoutput}" haltOnfailure="true">
                            
    <classfileset dir="${dir.target.bin.test}" includes="**/*.class" />
                            
    <classpath>
                                
    <pathelement location="${dir.src.test.resources}" />
                                
    <pathelement location="${dir.src.main.resources}" />
                                
    <pathelement location="${dir.target.bin.test}" />
                                
    <pathelement location="${dir.target.bin.main}" />
                                
    <fileset refid="ivy.cachefileset.test" />
                            
    </classpath>
                        
    </testng>
                    
    </then>
                    
    <else>
                        
    <echo>no testcase exist in "${dir.target.bin.test}", nothing to do for testng.</echo>
                    
    </else>
                
    </if>
            
    </sequential>
        
    </macrodef>

    注意這里有個檢測,判斷是否有測試案例存在,如果沒有寫測試案例,則跳過testng任務的執行,否則如果classfileset為空,testng即得不到testng.xml的輸入,也得不到classfileset的輸入,會直接報錯的,因此需要避免因為沒有測試案例導致test失敗進而整個build都失敗的情況。

    OK,上述三板斧下去,基本ant + ivy + testng就可以完成整合,一起跑起來了。敲一個ant test下去,就可以依賴解析,編譯,執行testcase的全套過程。過程比maven + junit復雜多了,主要是一切都要自己動手,不過完成之后的效果似乎還不錯。上述的過程對于一般項目都是通用的,因此以后就可以偷懶了。

    posted on 2010-05-31 16:11 sky ao 閱讀(2476) 評論(0)  編輯  收藏 所屬分類: project building

    主站蜘蛛池模板: 亚洲码在线中文在线观看| 中文字幕亚洲免费无线观看日本 | 亚洲成AV人片高潮喷水| 国产免费AV片在线播放唯爱网| 亚洲一区影音先锋色资源| 在线免费观看你懂的| 亚洲精品美女在线观看| 国产91免费在线观看| 亚洲午夜福利在线视频| 日韩在线免费电影| 免费的黄网站男人的天堂| 亚洲国产成人爱av在线播放| 久久九九免费高清视频| 久久精品国产亚洲| 嫖丰满老熟妇AAAA片免费看| 亚洲人成电影网站色www| 国产精品va无码免费麻豆| 午夜免费国产体验区免费的 | 亚洲阿v天堂在线| 91大神免费观看| 亚洲最大成人网色香蕉| 国产精品无码免费视频二三区| 牛牛在线精品免费视频观看| 自拍偷自拍亚洲精品情侣| 免费看又黄又无码的网站| 亚洲日本人成中文字幕| www亚洲精品少妇裸乳一区二区| 羞羞视频免费网站在线看| 久久精品国产亚洲AV无码麻豆 | 中国人xxxxx69免费视频| 中文字幕亚洲综合久久综合| 亚洲精品专区在线观看| 久久成人无码国产免费播放| 亚洲七久久之综合七久久| 中文字幕亚洲无线码| 台湾一级毛片永久免费| 一级做a爰片久久毛片免费陪 | 亚洲成年网站在线观看| 亚洲国产精品丝袜在线观看| 91热久久免费精品99| 男人免费视频一区二区在线观看|