Android添加單元測試的方法與步驟

來源:互聯網
上載者:User

一、修改設定檔AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.ycmoon.test.activity" android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <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>
    <instrumentation android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="cn.ycmoon.test.activity" android:label="Testing" />
</manifest>

說明:1、在Application節點中Activity前添加   <uses-library android:name="android.test.runner" />

2、在Application節點的添加  <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.ycmoon.test.activity" android:label="Testing" />

 二、在相應包中編寫要完方法,

 

 

三、在src同級級目錄中添加一個test檔案夾[可自訂],並在下建立和項目一致的包名: cn.ycmoon.test.activity

 四、在要進行單元測試的方法上右鍵->NEW->JUNIT Test Case。彈出下介面:

選擇 SuperClass為:

 

 點擊下一步,選擇要測試的方法:

 

 點擊完成。

 將新產生的單元測試的類放入test/項目包名下。

 

在單元測試方法中添加實現:

 展開test/cn.ycmoon.test.activity下單元測試類,選擇要測試的方法,點右鍵,選擇Run As->Android JUnit Test,就會開啟單元測試結果介面。

右該方法無錯誤,則結果會顯示綠色狀態條:

 

相關文章

聯繫我們

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