可持续集成测试


最近在搞可持续集成测试,顺便写个小例子以后方便参考
目录结构
src\放程序代码
src\test  放testcase
使用的软件版本
pmd:4.2.5
cobertura-1.9.4
findbugs-1.3.9
编译的目录结构
build\classes  源代码*.class
build\test      testcase *.class
build\report    测试报告
构建平台
Jenkins

待测代码
public class Hel {
	
	public int sayHi(int m ){	 
		int i = 5+m;
		return i;
	}
	 
}

testcase

public class TestHel extends TestCase{
 
	public void testHel() throws Exception { 
		 Hel hel = new Hel();
		 int a = hel.sayHi(5);
		 assertEquals(10,a);
	}
	
}



build.xml
<?xml version="1.0" encoding="utf-8"?>

<project name="tproject" basedir="." default="main">
	
	<property environment="env" />
	<property name="project.root" value="${basedir}" />
	<property file="build.properties" />
	
	
	<!-- 代码目录,建议使用下面的默认值 -->
	<property name="src.dir" value="${project.root}/src" />
	<property name="src.java.dir" value="${src.dir}/java" />
	<property name="src.test.dir" value="${src.dir}/test" />
	
	<property name="build.dir" value="${project.root}/build" />
	<property name="build.lib.dir" value="${project.root}/lib" />
	<property name="build.classes.dir" value="${build.dir}/classes" />
	<property name="build.test.dir" value="${build.dir}/test" />
	<property name="build.report.dir" value="${build.dir}/report" />
	
	<property name="build.junit-report.html.dir" value="${build.report.dir}/junit/html" />
	<property name="build.junit-report.xml.dir" value="${build.report.dir}/junit/xml" />
	<property name="build.pmd-report.xml.dir" value="${build.report.dir}/pmd/xml" />
	
	<property name="build.findbugs-report.xml.dir" value="${build.report.dir}/findbugs/xml" />
	
	<!--代码覆盖率变量-->
	<property name="cobertura.dir" value="D:/cobertura-1.9.4" />
	<!--findbugs 代码检测-->
	<property name="findbugs.home" value="D:/findbugs-1.3.9"/>
	<!--pmd 代码检测-->
	<property name="pmd.home" value="D:/pmd-4.2.5"/>
	
	<property name="instrumented.dir" value="${build.dir}/report/instrumented"/>
	<property name="build.coberturas-report.xml.dir" location="${build.dir}/report/coverage/xml"/>
	
	<path id="master-classpath">
		<fileset dir="${build.lib.dir}" />
	</path>
	<!--代码覆盖率变量-->
	<path id="coberturas.classpath">  
		<fileset dir="${cobertura.dir}">  
			<include name="cobertura.jar" />  
			<include name="lib/*.jar" />  
		</fileset>  
	</path>  
	<!--测试类classpath-->
	<path id="test.run.classpath">
		<pathelement path="${build.test.dir}"/>	
	</path>
	<!--findbugs 代码检测-->
	<path id="findbug.path">  
			<fileset dir="${findbugs.home}\lib">  
				<include name="findbugs-ant.jar" />  
			</fileset>
	</path>
	<!--PMD 代码检测-->
	<path id="pmd.path">
		<fileset dir="${pmd.home}\lib">
			<include name="pmd-4.2.5.jar"/>
		</fileset>
	</path>
	<!--插针操作 环境变量设置-->
	<path id="cobertura.classpath">  
		    <fileset dir="${cobertura.dir}">  
		       <include name="cobertura.jar" />  
		       <include name="lib/*.jar" />  
		    </fileset>  
	</path> 
	<target name="main" depends="usage,clean,init,compile,instrument,pmd,findbugs,runTestCase,coverage-report" description="启动测试用例"/>
	
	<!-- =================================================================== -->
	<!-- 清空所有输出文件包括build和部署目录 -->
	<!-- =================================================================== -->
	<target name="clean" description="清空所有输出文件包括build和部署目录">
		<delete dir="${build.dir}" />
	</target>
	<!-- =================================================================== -->
	<!-- 初始化,创建目录 -->
	<!-- =================================================================== -->
	<target name="init" description="创建目录" >
		<mkdir dir="${build.classes.dir}" />
		<mkdir dir="${build.test.dir}" />
		<mkdir dir="${build.report.dir}" />
		<mkdir dir="${instrumented.dir}" />
		<mkdir dir="${build.junit-report.html.dir}" />
		<mkdir dir="${build.junit-report.xml.dir}" />
		<mkdir dir="${build.pmd-report.xml.dir}" />
		<mkdir dir="${build.coberturas-report.xml.dir}" />
		<mkdir dir="${build.findbugs-report.xml.dir}" />
	</target>
	
	<!-- =================================================================== -->
	<!-- 编译 -->
	<!-- =================================================================== -->
	<target name="compile" description="编译Java文件">
		<mkdir dir="${build.dir}" />
		<javac destdir="${build.classes.dir}" target="1.6" fork="true" debug="true" deprecation="false" optimize="false" failonerror="true">
			<src path="${src.java.dir}" />
		</javac>
		<javac destdir="${build.test.dir}"  target="1.6" fork="true" debug="true" deprecation="false" optimize="false" failonerror="true">
			<src path="${src.java.dir}" />
			<src path="${src.test.dir}" />
			<classpath refid="master-classpath" />
		</javac>
	</target>
	<!-- =================================================================== -->
	<!-- 执行插针操作 -->
	<!-- =================================================================== -->
	<taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>  
	<target name="instrument" depends="init,compile">
		<cobertura-instrument todir="${instrumented.dir}">  
			 <ignore regex="org.apache.log4j.*" /> 
			   <fileset dir="${build.dir}">  
			   		<include name="**/*.class"/>  
			    	<exclude name="**/Test*.class" />  
			   </fileset>  
	    </cobertura-instrument>
	</target>
	<!-- =================================================================== -->
	<!-- 执行测试案例 -->
	<!-- =================================================================== -->
	<fail if="test.failed">Test is failed!</fail>
	<target name="runTestCase"  description="执行测试用例">
		<echo message="runTestCase start...." />		 
		<junit errorproperty="test.failed" failureproperty="test.failed" fork="true" maxmemory="1024M" forkmode="perBatch">	
			<classpath location="${instrumented.dir}" />
			<classpath location="${build.dir}" />
			<classpath refid="test.run.classpath" /> 
			<classpath refid="coberturas.classpath" /> 	
			<formatter type="xml" />
			<batchtest todir="${build.junit-report.xml.dir}" unless="testcase">
				<resources>
					<fileset dir="${build.test.dir}">
						<include name="**/Test*" />
					</fileset>
				</resources>
			</batchtest>
		</junit>
		<antcall target="xmlToHtml" />
		<echo message="runTestCase end...." />
	</target>
	<target name="xmlToHtml" description="xml转为html">
			<echo message="xmlToHtml start...." />
			<junitreport todir="${build.junit-report.html.dir}">
				<fileset dir="${build.junit-report.xml.dir}">
					<include name="TEST-*.xml" />
				</fileset>

				<report format="frames" todir="${build.junit-report.html.dir}" />
			</junitreport>
			<echo message="xmlToHtml end...." />
	</target>
	<!-- =================================================================== -->
	<!-- 代码覆盖率报告生成 -->
	<!-- =================================================================== -->
	<target name="coverage-report"  description="生成代码覆盖率文档">
		<cobertura-report srcdir="${src.java.dir}"  destdir="${build.coberturas-report.xml.dir}" format="xml" />
	</target>
	
	<!-- =================================================================== -->
	<!-- PMD 代码检测 -->
	<!-- =================================================================== -->
	<taskdef name="pmd" classpathref="pmd.path" classname="net.sourceforge.pmd.ant.PMDTask"/>
	<target name="pmd" >
		<echo message="pmd start...."/>
			<pmd shortFilenames="true">
				 <ruleset>rulesets/design.xml</ruleset>
				 <ruleset>rulesets/strings.xml</ruleset>
				<ruleset>rulesets/naming.xml</ruleset>
				  <ruleset>basic</ruleset>
				<formatter type="html" toFile="${build.pmd-report.xml.dir}/pmd.html" toConsole="true"/>
				<fileset dir="${src.java.dir}"> 
					<include name="**/*"/>  
				</fileset>
			</pmd>
			<echo message="pmd end...."/>
	</target>
	<!-- =================================================================== -->
	<!-- findbugs 代码检测 -->
	<!-- =================================================================== -->
	<taskdef name="findbugs" classpathref="findbug.path" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" />  
		<target name="findbugs" >	
			<echo message="findbugs start...."/>
			  <findbugs home="${findbugs.home}" 
			    	output="xml"   
			    	outputFile="${build.findbugs-report.xml.dir}\findbugs.xml" 
			    	jvmargs="-Xms1024m -Xmx1024m">
			    <sourcePath path="${src.java.dir}" />
			    <class location="${build.classes.dir}" />
			  </findbugs>
			<echo message="findbugs end...."/>
		
	</target>
	<!-- =================================================================== -->
	<!-- 帮助信息 -->
	<!-- =================================================================== -->
	<target name="usage">
		<echo message="tproject junit findbugs PMD" />
		<echo message="用法:ant -[target]" />
		<echo message="------------------------------------------------------"             />
		<echo message="[target]"                                                           />
		<echo message="1.clean                               -->清除构建目录"               />
		<echo message="2.init                                -->构建初始化"                 />
		<echo message="3.compile                             -->编译程序"                   />
		<echo message="4.instrument                          -->插针操作"                   />
		<echo message="5.pmd                                 -->执行pmd代码检测"            />
		<echo message="6.findbugs                            -->执行findbugs代码检测"       />
		<echo message="7.runTestCase                         -->findbugs测试案例代码覆盖率" />
		<echo message="8.coverage-report                     -->生成代码覆盖率文档"         />
		<echo message="------------------------------------------------------"             />
	</target>
</project>

你可能感兴趣的:(ant,Jenkins,JUnit,cobertura,testcase)