cobertura 在websphere中的使用

本文介绍如何将代码覆盖率工具Cobertura与Websphere中间件集成,包括源代码编译、代码仪器化、测试执行、覆盖率报告生成及多份覆盖率数据合并等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   关于cobertura的介绍可参考我转载的几篇文章,这里就不做介绍了.这里我想介绍一下cobertura和中间件的结合使用。
    我们的项目是一个J2EE的项目,涉及了spring,hibernate,struts,EJB。部署在websphere上,本地的开发环境是IBM RAD。项目组自己开发了一个基于JUnit的测试框架,可以通过JSP调用测试类得出测试结果。美中不足的是该框架得不到代码覆盖率。于是我们决定引入cobertura.
    分析cobertura自带的example的bulid.xml我们可以将其分解成几个步骤:
   1.编译源代码,该步骤其实IDE已经替我们完成了,不需要通过ant去编译,所以省去.
Java代码 复制代码  收藏代码
  1. <target name="compile"  depends="init">   
  2.         <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">   
  3.             <classpath refid="cobertura.classpath" />   
  4.         </javac>   
  5.     </target>  
<target name="compile"  depends="init">
		<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
			<classpath refid="cobertura.classpath" />
		</javac>
	</target>

   2.instrument,在这里cobertura生成了instrument后的源代码。并生成cobertura.ser信息文件,该文件很重要,包含了需要被测试的类的信息。
    
Java代码 复制代码  收藏代码
  1. <target name="instrument" depends="init,compile">   
  2.         <!--   
  3.             Remove the coverage data file and any old instrumentation.   
  4.         -->   
  5.         <delete file="cobertura.ser"/>   
  6.         <!--<delete dir="${instrumented.dir}" />   
  7. -->   
  8.         <!--   
  9.             Instrument the application classes, writing the   
  10.             instrumented classes into ${build.instrumented.dir}.   
  11.         -->   
  12.         <cobertura-instrument  todir="${instrumented.dir}">   
  13.             <!--   
  14.                 The following line causes instrument to ignore any   
  15.                 source line containing a reference to log4j, for the   
  16.                 purposes of coverage reporting.   
  17.             -->   
  18.             <ignore regex="org.apache.log4j.*" />   
  19.   
  20.             <fileset dir="${classes.dir}">   
  21.                 <!--   
  22.                     Instrument all the application classes, but   
  23.                     don't instrument the test classes.   
  24.                 -->   
  25.                 <include name="**/*.class" />   
  26.                 <exclude name="**/*Test.class" />   
  27.             </fileset>   
  28.         </cobertura-instrument>   
  29.     </target>  
<target name="instrument" depends="init,compile">
		<!--
			Remove the coverage data file and any old instrumentation.
		-->
		<delete file="cobertura.ser"/>
		<!--<delete dir="${instrumented.dir}" />
-->
		<!--
			Instrument the application classes, writing the
			instrumented classes into ${build.instrumented.dir}.
		-->
		<cobertura-instrument  todir="${instrumented.dir}">
			<!--
				The following line causes instrument to ignore any
				source line containing a reference to log4j, for the
				purposes of coverage reporting.
			-->
			<ignore regex="org.apache.log4j.*" />

			<fileset dir="${classes.dir}">
				<!--
					Instrument all the application classes, but
					don't instrument the test classes.
				-->
				<include name="**/*.class" />
				<exclude name="**/*Test.class" />
			</fileset>
		</cobertura-instrument>
	</target>


   3.test ,这步是生成详细代码覆盖信息的步骤,并生成JUnit的测试结果。这里需要用instrument后的*.class替代原来的*.class,当instrument后的*.class被执行时,它们会纪录被调用的信息,并写入之前生成的信息文件cobertura.ser。
Java代码 复制代码  收藏代码
  1. <target name="test" depends="init,compile">   
  2.         <junit fork="yes" dir="${basedir}" failureProperty="test.failed">   
  3.             <!--   
  4.                 Note the classpath order: instrumented classes are before the   
  5.                 original (uninstrumented) classes.  This is important.   
  6.             -->   
  7.             <classpath location="${instrumented.dir}" />   
  8.             <classpath location="${classes.dir}" />   
  9.   
  10.             <!--   
  11.                 The instrumented classes reference classes used by the   
  12.                 Cobertura runtime, so Cobertura and its dependencies   
  13.                 must be on your classpath.   
  14.             -->   
  15.             <classpath refid="cobertura.classpath" />   
  16.   
  17.             <formatter type="xml" />   
  18.             <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />   
  19.             <batchtest todir="${reports.xml.dir}" unless="testcase">   
  20.                 <fileset dir="${src.dir}">   
  21.                     <include name="**/*Test.java" />   
  22.                 </fileset>   
  23.             </batchtest>   
  24.         </junit>   
  25.   
  26.         <junitreport todir="${reports.xml.dir}">   
  27.             <fileset dir="${reports.xml.dir}">   
  28.                 <include name="TEST-*.xml" />   
  29.             </fileset>   
  30.             <report format="frames" todir="${reports.html.dir}" />   
  31.         </junitreport>   
  32.     </target>  
<target name="test" depends="init,compile">
		<junit fork="yes" dir="${basedir}" failureProperty="test.failed">
			<!--
				Note the classpath order: instrumented classes are before the
				original (uninstrumented) classes.  This is important.
			-->
			<classpath location="${instrumented.dir}" />
			<classpath location="${classes.dir}" />

			<!--
				The instrumented classes reference classes used by the
				Cobertura runtime, so Cobertura and its dependencies
				must be on your classpath.
			-->
			<classpath refid="cobertura.classpath" />

			<formatter type="xml" />
			<test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
			<batchtest todir="${reports.xml.dir}" unless="testcase">
				<fileset dir="${src.dir}">
					<include name="**/*Test.java" />
				</fileset>
			</batchtest>
		</junit>

		<junitreport todir="${reports.xml.dir}">
			<fileset dir="${reports.xml.dir}">
				<include name="TEST-*.xml" />
			</fileset>
			<report format="frames" todir="${reports.html.dir}" />
		</junitreport>
	</target>


4.生成代码覆盖率报告。
Java代码 复制代码  收藏代码
  1. <target name="coverage-check">   
  2.         <cobertura-check branchrate="34" totallinerate="100" />   
  3.     </target>   
  4.   
  5.     <target name="coverage-report">   
  6.         <!--   
  7.             Generate an XML file containing the coverage data using   
  8.             the "srcdir" attribute.   
  9.         -->   
  10.         <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />   
  11.     </target>   
  12.   
  13.     <target name="alternate-coverage-report">   
  14.         <!--   
  15.             Generate a series of HTML files containing the coverage   
  16.             data in a user-readable form using nested source filesets.   
  17.         -->   
  18.         <cobertura-report destdir="${coverage.html.dir}">   
  19.             <fileset dir="${src.dir}">   
  20.                 <include name="**/*.java"/>   
  21.             </fileset>   
  22.         </cobertura-report>   
  23.     </target>  
<target name="coverage-check">
		<cobertura-check branchrate="34" totallinerate="100" />
	</target>

	<target name="coverage-report">
		<!--
			Generate an XML file containing the coverage data using
			the "srcdir" attribute.
		-->
		<cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
	</target>

	<target name="alternate-coverage-report">
		<!--
			Generate a series of HTML files containing the coverage
			data in a user-readable form using nested source filesets.
		-->
		<cobertura-report destdir="${coverage.html.dir}">
			<fileset dir="${src.dir}">
				<include name="**/*.java"/>
			</fileset>
		</cobertura-report>
	</target>



经过分析,这些步骤中只有第三步test部分执行有问题,因为没有websphere的环境,所以我决定将第三步分离,通过项目自带的JUnit框架来调用测试类。首先我执行instrument步骤,然后启动websphere服务器。在这里,正式服务器和RAD自带的websphere有所区别。正式的服务器需要用instrument后的*.class替换原来的,打包后重新发布。测试服务器上我采用先启动,然后替换发布后的*.class。然后我通过测试框架在jsp上执行测试类,此时,所有的信息并没有写入到相应的cobertura.ser文件中,只有当server停止时,系统才会将信息写入文件。因为在测试框架执行instrument后的class时,我们无法指定信息文件的位置,所以会在C:\RAD7\SDP70\runtimes\base_v61\profiles\serverName 文件夹下生成新的cobertura.ser文件,serverName为配置的server名称。然后再利用cobertura自带的merge功能将这两个cobertura.ser文件合并.

5.merge
Java代码 复制代码  收藏代码
  1. <target name="merge" description="merge files">   
  2.     <cobertura-merge>   
  3.         <fileset dir="${basedir}">   
  4.             <include name="**/cobertura1.ser"/>   
  5.             <include name="**/cobertura22.ser"/>   
  6.         </fileset>   
  7.     </cobertura-merge>   
  8. </target>   
	<target name="merge" description="merge files">
		<cobertura-merge>
		    <fileset dir="${basedir}">
		        <include name="**/cobertura1.ser"/>
		    	<include name="**/cobertura22.ser"/>
		    </fileset>
		</cobertura-merge>
	</target>	

最后,我们再通过ant生成相应的代码覆盖率的报告.这样,我们就完成了cobertura和中间件的结合.
PS:各种中间件生成cobertura.ser的位置有所不同.但都可以通过这种方式使用cobertura.
<think>好的,我现在需要回答用户关于如何在IntelliJ IDEA中使用Cobertura插件进行代码覆盖率分析的问题。首先,我需要确认用户的问题是什么。用户提到的是Cobertura插件,但根据提供的引用资料,引用中主要提到的是JaCoCo和IDEA自带的覆盖率工具,而不是Cobertura。所以可能需要先指出这一点,并说明Cobertura可能不被直接支持,但可以提供替代方案。 接下来,我需要检查引用内容。引用[1]和[3]提到IntelliJ IDEA内置的代码覆盖率工具,以及如何配置Coverage runner。引用[2]提到JaCoCo被多种工具集成,包括IDEA。引用[4]和[5]讨论了代码覆盖率的工具和测试覆盖的重要性。但所有引用都没有提到Cobertura在IDEA中的使用,这说明可能需要引导用户使用JaCoCo或者IDEA自带的工具,或者提供可能的替代方法。 可能用户混淆了Cobertura和JaCoCo,或者希望使用Cobertura。但根据现有资料,IDEA可能更支持JaCoCo。因此,在回答中需要说明这一点,并建议使用JaCoCo,同时如果用户确实需要使用Cobertura,可能需要通过其他方式集成,比如Maven或Gradle插件,然后在IDEA中查看报告。 然后,按照用户的要求,回答需要结构清晰,分步骤说明。同时,用户要求生成相关问题,我需要准备三个相关问题。 现在,我需要组织步骤,可能的步骤包括:安装插件(如果有的话),配置构建工具(如Maven),生成覆盖率报告,以及在IDEA中查看结果。但根据引用,IDEA本身可能没有直接支持Cobertura的插件,而更多是JaCoCo。因此,可能需要指导用户使用外部工具生成报告,然后在IDEA中打开。 此外,需要确保使用正确的LaTeX格式,行内公式用$...$,独立公式用$$...$$,但这个问题可能不涉及数学公式,所以可能不需要使用。但需要检查是否有需要的地方,例如如果有提到算法中的数学表达式,但这里主要是配置步骤,可能不涉及。 最后,生成相关问题,例如关于JaCoCo的使用,IDEA内置工具,或者代码覆盖率的优化方法。</think>### IntelliJ IDEA 使用 Cobertura 插件进行代码覆盖分析教程 目前 **IntelliJ IDEA 官方未直接集成 Cobertura 插件**,但可通过以下两种方式实现代码覆盖率分析: --- ### 方法一:使用 IDEA 内置覆盖率工具(推荐) IDEA 自带了覆盖率分析功能,支持 Java、Kotlin 等语言,操作更便捷: 1. **配置覆盖率工具** - 打开 `File > Settings > Build, Execution, Deployment > Coverage`,选择默认的覆盖率工具(如 JaCoCo)[^1][^3]。 - 调整采样率或过滤不需要分析的类。 2. **运行测试并生成报告** - 右键点击测试类或方法,选择 `Run 'Tests' with Coverage`。 - 结果会在 `Coverage` 工具窗口显示,包括行覆盖率、分支覆盖率等[^5]。 3. **导出报告** - 在 `Coverage` 窗口点击 `Export`,生成 HTML 或 XML 格式报告。 --- ### 方法二:通过构建工具集成 Cobertura 若需强制使用 Cobertura,需借助 Maven/Gradle 生成报告后导入 IDEA: 1. **添加 Cobertura 插件到项目** - **Maven** 在 `pom.xml` 中添加: ```xml <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.7</version> </plugin> ``` - **Gradle** 在 `build.gradle` 中添加: ```groovy apply plugin: 'net.saliman.cobertura' ``` 2. **生成覆盖率报告** 执行命令: ```bash mvn cobertura:cobertura # Maven gradle cobertura # Gradle ``` 报告会生成在 `target/site/cobertura`(Maven)或 `build/reports/cobertura`(Gradle)。 3. **在 IDEA 中查看报告** - 使用浏览器打开生成的 `index.html` 文件,或通过 IDEA 的 `Tools > Open Generated Sources` 定位报告。 --- ### 对比建议 - **内置工具(JaCoCo)**:集成度高,实时反馈,适合快速分析[^2]。 - **Cobertura**:需手动配置,适合历史项目或特定兼容性需求。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值