Software engineering jobs fifth describes how to use test tools in the project

Work requirements:

In the software testing section, we introduce a lot of VSTS software testing tools, testing tools use on other platforms, and write blog describes how specific use in your project.

 

The job I use JUnit, Ant, SourceMonitor, findbugs testing tools such as the following brief description about the role of these tools.

JUnit: Java language is a unit testing framework. Briefly, a tool is used to test certain classes or methods.

Ant: As long used Linux system, readers should know that make this command. When compiling the Linux kernel and some software source code , often use this command. Make command is actually a project management tool , and achieved Ant function similar to this.

SourceMonitor: detecting a code complexity.

findbugs: bug detection code.

 

table of Contents

 

I. Item Description ..................................... 3

II. Summary of test ..................................... 3

2.1, the test environment ................................. 3

2.2 Test method ................................. 3

III. Code ......................................... 3

3.1, the program code ................................. 3

3.2, 4 test codes .............................

3.3, build.xml codes ........................... 5

IV. Coverage analysis ..................................... 8

Fives. Ant automatically generated reports ........................... 9

six. SourceMonitor detection code complexity ................ 10

Seven. findbugs test reports ........................... 10

VIII. Test Summary .................................... 11

 

A . Item Description

In designing the program, if the condition statement is often used to select a sentence, often to occur if the select statement segment to segment in many types of conditions occur when mixed, although if statement written in simple but easily lead to duplication and lack of conditions, Therefore, the detection unit using statement if the statement is correct or not is very important. In our project has also applied to the condition if the select statement. Therefore, this specific code detecting section comprising a plurality of the if statement, for the purpose of testing unit.

II. Test Summary

2.1 , the test environment

Eclipse,Junit

2.2 , the test method

Unit testing ( module test ) is a small piece of code developers to write for a small test code under test, it is clear function correctly. Typically, a test unit for judging a specific condition (or scene) acts at a particular function.

III. Code

3.1 , the program code

The following code is extracted from the project section wherein

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 , test the code

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 , the build.xml Code

<?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>

IV. Coverage Analysis

Fives. Ant automatically generated reports

 

 

 

 

 

 

 

 

Six. SourceMonitor detection code complexity

 

 

 

 

Seven. Findbugs test report

After installing findbugs open findbugs the project right, observe Bug explorer window

 

 

Yellow small insects, are warning bug (black bug logo are classified; red bug flag indicates a serious bug, you must modify the code after the discovery; orange bug flag indicates a potential warning bug, should be modified;) suggested that the class name should begin with a capital letter use, follow the prompts to change. The results are as follows

 

 

No bugs appeared after findbugs again, perfect bug fixes

VIII. Test Summary

1. In the test script: test methods to test for the beginning to

2. Test method to identify with @test. @BEFORE logo initialization method

3. The benefits of unit testing: a module error does not affect the other modules running tests

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

Guess you like

Origin www.cnblogs.com/jiangfenli/p/10979137.html