Android junit單元測試,androidjunit

來源:互聯網
上載者:User

Android junit單元測試,androidjunit

軟體測試的分類
* 黑箱測試
* 測試邏輯業務
* 白盒測試
* 測試邏輯方法

根據測試粒度
* 方法測試:function test
* 單元測試:unit test
* 整合測試:integration test
* 系統測試:system test

根據測試暴力程度
* 煙霧測試 (Smoke Test):smoke test
* 壓力測試:pressure test

建立android項目,建立Test.java檔案,注意定義一個類繼承一定要繼承AndroidTestCase

package com.wuyudong.juint.test;import com.wuyudong.juint.util.Utils;import android.test.AndroidTestCase;public class Test extends AndroidTestCase {        public void test() {        int res = Utils.add(3, 5);        assertEquals(8, res);    }}

建立工具包檔案Utils.java

package com.wuyudong.juint.util;public class Utils {    public static int add(int a, int b) {        return a - b;    }}

運行項目,報錯:

[2016-05-30 06:21:13 - 單元測試] 單元測試 does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml

在AndroidManifest.xml中添加下面的代碼:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.wuyudong.juint"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />        <instrumentation         android:name="android.test.InstrumentationTestRunner"        android:targetPackage="com.wuyudong.juint"        ></instrumentation>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <uses-library             android:name="android.test.runner"/>        <activity            android:name="com.wuyudong.juint.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

繼續運行單元測試test,出現下面的斷言異常

雙擊跳轉到

public class Test extends AndroidTestCase {        public void test() {        int res = Utils.add(3, 5);        assertEquals(8, res);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.