Junit测试工具

1.测试环境java

Junitandroid

2.测试说明程序员

单元测试(最微小规模的测试;以测试某个功能或代码块。典型地由程序员而非测试员来作,由于它须要知道内部程序设计和编码的细节知识。这个工做不容易作好,除非应用系统有一个设计很好的体系结构; 还可能须要开发测试驱动器模块或测试套具。)app

3.测试方法框架

咱们新建一个项目,模板代码会默认在build文件中添加JUnit的依赖,而单元测试代码是放在src/test/java下面的。
用鼠标右键点击测试方法,选择菜单中的“Run”选项就能够执行对应的单元测试。
JUnit介绍
JUnit是Java最基础的测试框架,主要的做用就是断言。
使用时在app的build文件中添加依赖。注意:用于测试环境框架一概是testCompile开头。
 
dependencies {
    testCompile 'junit:junit:4.12'
}
4.测试代码

 

importjava.util.Scanner;单元测试

importjavax.script.ScriptEngine;测试

importjavax.script.ScriptEngineManager;ui

importjavax.script.ScriptException;this

importorg.junit.Before;编码

importorg.junit.Test;

public class carteenmanagesystem {

       @Before

       publicvoidsetUp() throws Exception {

       }

       @Test

       publicvoidtest() {

              //fail("Not yet implemented");

              ScriptEngineManagersem = newScriptEngineManager();

              ScriptEnginese = sem.getEngineByName("js");

         Scanner sc = newScanner(System.in);

              while(true)

        {

                     System.out.print("输入你的对餐厅服务的意见");

            String line = sc.nextLine().trim();

              try

            {

                Object result = se.eval(line);

                            System.out.println(line);

            }

              catch(ScriptExceptione)

            {

                            System.out.print("意见不能为空");

                            sc.close();

                            break;

                      }

        }

}

}

5.详细用法
执行顺序:@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
1.首先测试 stampToDate方法,测试时我认为应该返回的结果等于最终的结果是否相同。
2.接下来测试 dateToStamp方法。
3.咱们注意到在 dateToStamp方法中,有抛出一个解析异常(ParseException)。也就是当参数没有按照规定格式去传,就会致使这个异常。
验证一个方法是否抛出了异常?能够给 @Test注解设置 expected参数来实现
4.抛出了对应的异常则测试成功,反之则测试失败。

build.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!-- WARNING: Android 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 <?android.ant.import?>

              as the first entry and export the buildfile again. --><project basedir="." default="build" name="testone">

<property environment="env"/>

<propertyname="ANDROID_HOME" value="../../android/"/>

<property name="debuglevel" value="source,lines,vars"/>

<property name="target" value="1.8"/>

<property name="source" value="1.8"/>

<path id="testone.classpath">

<pathelement location="bin"/>

</path>

<target name="init">

<mkdirdir="bin"/>

<copy includeemptydirs="false" todir="bin">

<filesetdir="src">

<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"/>

<classpathrefid="testone.classpath"/>

</javac>

</target>

<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>

<target description="copy Android compiler jars to ant lib directory" name="init-android-compiler">

<copy todir="${ant.library.dir}">

<filesetdir="${ANDROID_HOME}/plugins" includes="org.android.jdt.core_*.jar"/>

</copy>

<unzip dest="${ant.library.dir}">

<patternset includes="jdtCompilerAdapter.jar"/>

<filesetdir="${ECLIPSE_HOME}/plugins" includes="org.android.jdt.core_*.jar"/>

</unzip>

</target>

<target description="compile project with Android compiler" name="build-android-compiler">

<property name="build.compiler" value="org.android.jdt.core.JDTCompilerAdapter"/>

<antcall target="build"/>

</target>

<target name="carteenmanagesystem">

<java classname="teston.carteenmanagesystem" failonerror="true" fork="yes">

<classpathrefid="testone.classpath"/>

</java>

</target>

</project>

相关文章
相关标签/搜索