junit

来源:互联网 发布:spyder python 使用 编辑:程序博客网 时间:2024/04/19 01:19

v 运行JUnit

Ø 下载junit.zip,解压缩目录如:

       D:/tools/develope/junit>

Ø执行命令

       java-cp junit.jar;. junit.textui.TestRunner junit.samples.AllTests

       java-cp junit.jar;. junit.swingui.TestRunner junit.samples.AllTests

 

v JUnit核心

Ø JUnitTestCaseTestSuiteBaseTestRunner3个骨干类组成。

Ø TestRunner来运行测试

Ø TestSuite来组合测试

Ø TestCase来执行单个测试

 

v 模式使用

Ø Composite模式:往TestSuite增加一个对象时,增加的是Test接口的实现类(TestCase

       TestSuite均实现了Test接口)

ØCommand模式:Test接口提供公共的Run方法供其子类使用。

 

v TestResult

Ø TestResult负责收集TestCase的执行结果,如果预期结果不对,JUnit就会创建一个TestFailure对象,它会存储在TestResult中。TestRunner使用TestResult来报告测试结果,如果TestResult集会中没有TestFailure对象,那么代码就是干净的,进度条就用绿色表示(JUnit格言:keep the bar green to keep thecode clean)。

 

v Assert超类型的8个核心方法

ØassertTrue

ØassertFalse

ØassertEquals

ØassertNotNull

ØassertNull

ØassertSame

ØassertnotSame

Øfail

v ant实例

           <junitprintsummary="no"forkmode="once"errorProperty="test.failed"

                failureProperty="test.failed" fork="${junit.fork}">

                <classpath>

                    <pathelementpath="@{additional.src.dirs}"/>

                    <pathrefid="@{module}.test.classpath"/>

                    <pathelementlocation="${build.dir}/@{module}/classes"/>

                    <pathelementlocation="${test.dir}/@{module}/classes"/>

                    <pathelementpath="${java.class.path}"/>

                    <pathelementpath="${webapp.target}"/>

                    <pathelementpath="${build.dir}/dao/gen"/>

                    <!--For .properties and .xml files -->

                    <pathelementpath="${build.dir}/web/classes"/>

                    <pathelementpath="src/@{module}"/>

                </classpath>

                <formattertype="xml"/>

                <formattertype="brief"usefile="false"/>

                <batchtesttodir="${test.dir}/data"if="testcase">

                    <filesetdir="${test.dir}/@{module}/classes">

                        <include name="**/*${testcase}*"/>

                        <exclude name="**/*TestCase.class"/>

                    </fileset>

                </batchtest>

                <batchtesttodir="${test.dir}/data"unless="testcase">

                    <filesetdir="${test.dir}/@{module}/classes">

                        <include name="**/*Test.class*"/>

                    </fileset>

                </batchtest>

            </junit>

Ø printsummary="yes":在测试的最后一行输出一个单行的概要

Øfork=yes:强制ant对美国测试分别使用一个单独的Java虚拟机,这样避免了测试见的相

    互影响。

Øbatchtest:自动找出要运行的测试

Øhaltonfailurehaltonerror表示如果失败或产生错误将停止编译。

 

vJUnit打印好的报告

    <target name="test-reports"description="Generatetest reports">

        <mkdirdir="${test.dir}/reports"/>

        <junitreporttodir="${test.dir}">

            <filesetdir="${test.dir}/data">

                <includename="TEST-*.xml"/>

            </fileset>

            <reportformat="frames"todir="${test.dir}/reports"/>

        </junitreport>

        <echolevel="info">

        Test reports generated at ${test.dir}/reports

        </echo>

    </target>

v Maven安装

Ø下载Maven

Ø定义一个MAVEN_HOME环境变量,执行Maven安装目录

Ø Maver/bin添加到PATH环境中