Android中使用自身攜帶的Junit建立一個測試工程

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   ar   使用   

1、建立立一個Android工程

package com.shellway.junit;public class Service {      public int divide(int a,int b){          return a/b;      }}
Service.java.作為被測試類別
package com.shellway.junit;import junit.framework.Assert;import android.test.AndroidTestCase;public class TestT extends AndroidTestCase {    public void test1(){        Service service = new Service();        System.out.println(service.divide(10, 2));    }    public void test2(){        Service service = new Service();        System.out.println(service.divide(10, 0));    }    public void test3(){        Service service = new Service();        Assert.assertEquals(2.5, service.divide(10, 4));    }}
TestT繼承AndroidTestCase,作為測試類別
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.shellway.junit"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="16"        android:targetSdkVersion="21" />       <instrumentation        android:targetPackage="com.shellway.junit"        android:name="android.test.InstrumentationTestRunner" />        <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=".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>
AndroidManifest.xml

注意:在設定檔中要加入的內容是:

 <instrumentation      android:targetPackage="com.shellway.junit"      android:name="android.test.InstrumentationTestRunner" /><uses-library android:name="android.test.runner" />
View Code

並且要注意他們加入的位置。這是一種測試自身工程的方法。

另一種方法是:建立一個Android Junit工程來測試另外的一個工程,這裡需要注意的是建立這個測試工程名的時候不能以Test來命名,不然出現錯誤都很難找出問題所在,下面給出以Test來命名工程的出錯資訊。

 

Android中使用自身攜帶的Junit建立一個測試工程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.