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>

相關文章
相關標籤/搜索