Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Use JUnit CN

本文论述如何在Eclipse 中使用JUnit 进行单元测试。

基本概念

  • TestCase - 测试用例,子类继承它可以实现一个或多个测试用例。一般子类命名以Test 结尾,可以理解为单个类的测试用例集合。
  • TestSuite - 测试套件,子类继承它来组织已有的测试类。一般子类命名以Tests 结尾,可以理解为多个类的测试用例集合。
  • TestRunner - 测试用例或测试套件的运行器。Eclipse 里有自己的Runner 用以方便测试类的运行。

举例说明

  • 新建sample 项目,源文件夹为src。
  • 新建并实现Triangle 类,包名为com.ime.sample。
  • 新建源文件夹test。
  • 新建并实现TriangleTest 类,包名为com.ime.sample.test。
  • 使用JUnit 运行TriangleTest。
  • 重构TriangleTest 和Triangle 类。

与Ant 结合生成测试报告

  • 新建并实现build.xml 文件在sample 项目中。
  • 使用Ant 运行build.xml。
  • 在浏览器中查看测试报告。

文件的源代码

  • sample.Triangle:
package sample;

/**
 * This is Triangle class
 */
public class Triangle {
    // 定义三角形的三边
    protected long lborderA = 0;

    protected long lborderB = 0;

    protected long lborderC = 0;

    // 构造函数
    public Triangle(long lborderA, long lborderB, long lborderC) {

        this.lborderA = lborderA;

        this.lborderB = lborderB;

        this.lborderC = lborderC;

    }

    /**
     * 判断是否是三角形
     *
     * 是返回ture;不是返回false
     */
    public static boolean isTriangle(Triangle triangle) {
        boolean isTriangle = false;

        // 判断边界,大于0 小于或等于Long.MAX_VALUE,出界返回false
        if ((triangle.lborderA > 0 && triangle.lborderA <= Long.MAX_VALUE / 2)
                && (triangle.lborderB > 0 && triangle.lborderB <= Long.MAX_VALUE / 2)
                && (triangle.lborderC > 0 && triangle.lborderC <= Long.MAX_VALUE / 2)) {

            // 判断两边之和大于第三边
            if ((triangle.lborderA < (triangle.lborderB + triangle.lborderC))
                    && (triangle.lborderB < (triangle.lborderA + triangle.lborderC))
                    && (triangle.lborderC < (triangle.lborderA + triangle.lborderB))) {
                isTriangle = true;
            }

        }

        return isTriangle;
    }

    /**
     * 判断三角形类型
     *
     * 只有两条边相等的三角形返回字符串“等腰三角形”; 三边均相等的三角形返回字符串“等边三角形”;
     * 三边均不等的三角形返回字符串“不等边三角形”;
     */
    public static String getType(Triangle triangle) {
        String strType = "不是三角形 ";

        // 判断是否是三角形
        if (isTriangle(triangle)) {
            // 判断是否是等边三角形
            if (triangle.lborderA == triangle.lborderB
                    && triangle.lborderB == triangle.lborderC) {
                strType = "等边三角形";
            }
            // 判断是否是不等边三角形
            else if ((triangle.lborderA != triangle.lborderB)
                    && (triangle.lborderB != triangle.lborderC)
                    && (triangle.lborderA != triangle.lborderC)) {
                strType = "不等边三角形";
            }
            // 三角形既非三边全部相等,又非全部不等,只能是部分相等即等腰三角形
            else {
                strType = "等腰三角形";
            }
        }

        return strType;
    }
}
  • sample.TriangleTest:
package sample;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Tests about <code>Triangle</code> class.
 * 
 * @author yangyz@NEUSOFT
 */
public class TriangleTest extends TestCase {
    public Triangle triangle = null;

    public static Test suite() {
        return new TestSuite(TriangleTest.class);
    }

    /**
     * Test for Triangle#isTriangle().<p>
     * 
     * Setup: lborderA = 0; lborderB = 3; lborderC = 3<br>
     * Expected: method returns false
     */
    public void testIsTriangle01() {
        // setup
        triangle = new Triangle(0, 3, 3);

        // expected
        boolean expected = false;

        // execute
        boolean result = Triangle.isTriangle(triangle);

        // actual
        boolean actual = result;

        // assert
        assertEquals("While a=0; b=3; c=3 mehtod fails!", expected, actual);
    }

    /**
     * Test for Triangle#isTriangle().<p>
     * 
     * Setup: lborderA = 3; lborderB = 3; lborderC = 3<br>
     * Expected: method returns true
     */
    public void testIsTriangle02() {
        // setup
        triangle = new Triangle(3, 3, 3);

        // expected
        boolean expected = true;

        // execute
        boolean result = Triangle.isTriangle(triangle);

        // actual
        boolean actual = result;

        // assert
        assertEquals("While a=3; b=3; c=3 mehtod fails!", expected, actual);
    }

    /**
     * Test for Triangle#getType().<p>
     * 
     * Setup: lborderA = 0; lborderB = 3; lborderC = 3<br>
     * Expected: method returns "不是三角形"
     */
    public void testGetType01() {
        // setup
        triangle = new Triangle(0, 1, 1);

        // expected
        String expected = "不是三角形";

        // execute
        String result = Triangle.getType(triangle);

        // actual
        String actual = result;

        // assert
        assertEquals("While a=0; b=3; c=3 mehtod fails!", expected, actual);
    }

    /**
     * Test for Triangle#getType().<p>
     * 
     * Setup: lborderA = 3; lborderB = 4; lborderC = 5<br>
     * Expected: method returns "不等边三角形"
     */
    public void testGetType02() {
        // setup
        triangle = new Triangle(3, 4, 5);

        // expected
        String expected = "不等边三角形";

        // execute
        String result = Triangle.getType(triangle);

        // actual
        String actual = result;

        // assert
        assertEquals("While a=3; b=4; c=5 mehtod fails!", expected, actual);
    }

    /**
     * Test for Triangle#getType().<p>
     * 
     * Setup: lborderA = 3; lborderB = 3; lborderC = 3<br>
     * Expected: method returns "等边三角形"
     */
    public void testGetType03() {
        // setup
        triangle = new Triangle(3, 3, 3);

        // expected
        String expected = "等边三角形";

        // execute
        String result = Triangle.getType(triangle);

        // actual
        String actual = result;

        // assert
        assertEquals("While a=3; b=3; c=3 mehtod fails!", expected, actual);
    }

    /**
     * Test for Triangle#getType().<p>
     * 
     * Setup: lborderA = 3; lborderB = 3; lborderC = 5<br>
     * Expected: method returns "等腰三角形"
     */
    public void testGetType04() {
        // setup
        triangle = new Triangle(3, 3, 5);

        // expected
        String expected = "等腰三角形";

        // execute
        String result = Triangle.getType(triangle);

        // actual
        String actual = result;

        // assert
        assertEquals("While a=3; b=3; c=5 mehtod fails!", expected, actual);
    }

    // 执行测试
    public static void main(String[] args) {
        // Text ui 方式
        // junit.textui.TestRunner.run(suite());

        // Swing ui 方式
        junit.swingui.TestRunner.run(TriangleTest.class);

        // AWT ui 方式
        // junit.awtui.TestRunner.run(TriangleTest.class);
    }
}
  • build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="sample.useJunitTest" default="all" basedir=".">
    <!--定义依赖环境的属性-->
    <!--property file="build.properties" /-->

    <!--属性的定义-->
    <property name="jar.name" value="sample.jar" />
    <property name="src" value="src" />
    <property name="build" value="build" />
    <property name="jar.dir" value="${build}/jar" />
    <property name="classes" value="${build}/classes" />

    <!--定义单元测试的属性-->
    <property name="test.jar.name" value="sampletest.jar" />
    <property name="test.src" value="testsrc" />
    <property name="test.classes" value="${build}/testclasses" />
    <property name="test.report.dir" value="${build}/report" />

    <!--目标 -->
    <target name="all" depends="${jar.name}" />
    <target name="${jar.name}" depends="compile">
        <mkdir dir="${jar.dir}" />
        <jar jarfile="${jar.dir}/${jar.name}" basedir="${classes}" />
    </target>
    <target name="compile">
        <mkdir dir="${classes}" />
        <javac srcdir="${src}" destdir="${classes}" />
    </target>
    <target name="${test.jar.name}" depends="test.compile">
        <mkdir dir="${jar.dir}" />
        <jar jarfile="${jar.dir}/${test.jar.name}" basedir="${test.classes}" />
    </target>
    <target name="test.compile">
        <mkdir dir="${test.classes}" />
        <javac srcdir="${test.src}" destdir="${test.classes}">
            <classpath>
                <fileset dir="${jar.dir}">
                    <include name="*.jar" />
                </fileset>
            </classpath>
        </javac>
    </target>
    <target name="unit.test.report" depends="unit.test">
        <junitreport todir="${test.report.dir}">
            <fileset dir="${test.report.dir}">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="frames" todir="${test.report.dir}/html" />
        </junitreport>
    </target>
    <target name="unit.test" depends="${jar.name},${test.jar.name}">
        <mkdir dir="${test.report.dir}" />
        <junit printsummary="yes" haltonfailure="no">
            <classpath>
                <fileset dir="${jar.dir}">
                    <include name="*.jar" />
                </fileset>
            </classpath>
            <formatter type="xml" />
            <batchtest fork="yes" todir="${test.report.dir}">
                <fileset dir="${test.src}">
                    <include name="**/*Test.java" />
                </fileset>
            </batchtest>
        </junit>
    </target>
    <target name="clean">
        <delete dir="${build}" />
    </target>
</project>

使用测试框架进行插件的测试

  • 下载Automated Testing for Custom Plugins 包,并解压缩到自定义的目录下(如:G:\Custom-plugin-testing)。
  • 将解包的Eclipse SDK3.1.1 和Eclispe Test Framework3.1.1 解压缩到/eclipse-utp/ 目录下。
  • 将做成的Test plugin 和Tested plugin 及依赖放到/eclipse-custom/目录下。
  • 修改/label.properties 适配Build 信息。
  • 修改/tests.xml、/tests.properties 添加自定义插件的测试任务。
  • 双击/runt-cmd.bat,在命令行窗口中键入runtests [参数]运行测试。
  • 查看在/generate/results 目录下生成的测试报告。

Back to the top