作业要求:
在软件测试章节中中,我们介绍了不少VSTS的软件测试工具,请使用一些其他平台上的测试工具,并写博客介绍如何在你的项目中具体使用。
本次作业中我使用了JUnit、Ant、SourceMonitor、findbugs等测试工具,以下简单叙述一下这些工具的作用。
JUnit:是一个Java语言的单元测试框架。简单来说,是用来测试某些类或方法的一个工具。
Ant:只要使用过Linux系统的读者,应该知道 make这个命令。当编译Linux内核及一些软件的源程序时,经常要用这个命令。Make命令其实就 是一个项目管理工具,而Ant所实现功能与此类似。
SourceMonitor:用于检测代码复杂度。
findbugs:检测代码的bug。
目录
一.项目说明..................................... 3
二.测试概要..................................... 3
2.1、测试环境................................. 3
2.2、测试方法................................. 3
三.代码......................................... 3
3.1、程序代码................................. 3
3.2、测试用例代码............................. 4
3.3、build.xml代码........................... 5
四.覆盖分析..................................... 8
五.Ant自动生成的报告........................... 9
六.SourceMonitor检测代码复杂度................ 10
七.findbugs测试报告........................... 10
八.测试总结.................................... 11
一.项目说明
在设计程序时,if条件选择语句经常用到的一种语句,经常成段成段的出现if选择语句,在出现多类条件混合时,if语句虽然编写简单但是容易造成条件的重叠和缺失,所以,检测单元语句中if语句使用正确与否十分重要。在我们的项目中也运用到了if条件选择语句。因此,本文针对特定的一段含有多个if语句的代码进行检测,以达到单元测试的目的。
二.测试概要
2.1、测试环境
Eclipse,Junit
2.2、测试方法
单元测试(模块测试)是开发者编写的一小段代码,用于检验被测代码的一个很小的、很明确的功能是否正确。通常而言,一个单元测试是用于判断某个特定条件(或者场景)下某个特定函数的行为。
三.代码
3.1、程序代码
以下这段代码是从项目中抽取出来的其中一段
public class test {
public int judgeScore(int x) {
if(x>=90)
return 1;
else if(x>=80)
return 2;
else if(x>=70)
return 3;
else if(x>=60)
return 4;
else
return 5;
}
public int add(int x,int y) {
return (x+y);
}
}
3.2、测试用例代码
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import testJUnit.test;
public class testTest extends TestCase{
@Test
public void testjudgeScore() {
//fail("Not yet implemented");
test t=new test();
assertEquals(1,t.judgeScore(99));
assertEquals(2,t.judgeScore(85));
assertEquals(3,t.judgeScore(76));
assertEquals(4,t.judgeScore(63));
assertEquals(5,t.judgeScore(32));
}
@Test
public void testadd() {
test t1=new test();
assertEquals(190,t1.add(99,91));
}
}
3.3、build.xml代码
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build" name="testJUnit">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../../../eclipse/"/>
<property name="junit.output.dir" value="junit"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<path id="JUnit 4.libraryclasspath">
<pathelement location="${ECLIPSE_HOME}plugins/org.junit_4.12.0.v201504281640/junit.jar"/>
<pathelement location="${ECLIPSE_HOME}plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>
</path>
<path id="testJUnit.classpath">
<pathelement location="bin"/>
<path refid="JUnit 4.libraryclasspath"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="testJUnit.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="testTest">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml"/>
<test name="test.testTest" todir="${junit.output.dir}"/>
<jvmarg line="-ea"/>
<classpath refid="testJUnit.classpath"/>
</junit>
</target>
<target name="junitreport">
<junitreport todir="${junit.output.dir}">
<fileset dir="${junit.output.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${junit.output.dir}"/>
</junitreport>
</target>
</project>
四.覆盖分析
五.Ant自动生成的报告
六. SourceMonitor检测代码复杂度
七. Findbugs测试报告
安装好findbugs后对项目右键打开findbugs,观察Bug explorer窗口
有黄色小虫子,属于警告性bug(黑色的臭虫标志是分类;红色的臭虫标志表示严重bug,发现后必须修改代码; 橘黄色的臭虫标志表示潜在警告性bug,应尽量修改;)提示说类名应该使用大写字母开头,按照提示更改。结果如下
再次findbugs后没有出现虫子了,完美修复bug
八. 测试总结
1.在测试脚本中:测试的方法要以test为开头
2.测试方法用@test标识。@BEFORE标识初始化方法
3.单元测试好处:一个模块出错,不影响其他模块运行测试
4.遇到的错误:The method assertEquals(Object, Object) is ambiguous for the type testTest
解决途径:首先学习了解Assert类别
5.需要注意一个findbugs的版本问题,由于我安装的eclipse版本比较低,所以无法使用最新的findbugs,然后找了个旧版本的findbug。 http://sourceforge.net/project/showfiles.php?group_id=96405(旧版本Findbugs下载地址)
参考链接:https://www.cnblogs.com/zhaochifan/p/5201924.html