android 结合Junit进行测试.

学习android. 看看是否可以和 junit一起使用..

 

 

修改 AndroidManifest.xml

 

<!-- We add an application tag here just so that we can indicate that
         this package needs to link against the android.test library,
         which is needed when building test cases. -->    
    <application>
        <uses-library android:name="android.test.runner" />
    </application>

    <!--
    This declares that this app uses the instrumentation test runner targeting
    the package of com.example.android.apis.  To run the tests use the command:
    "adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner"
    -->
    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="com.example.android.apis"
                     android:label="Tests for Api Demos."/>
 

找到官方的apidemo 发现里面有测试

 

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis;

import junit.framework.Test;
import junit.framework.TestSuite;

import android.test.suitebuilder.TestSuiteBuilder;

/**
 * A test suite containing all tests for ApiDemos.
 *
 * To run all suites found in this apk:
 * $ adb shell am instrument -w \
 *   com.example.android.apis.tests/android.test.InstrumentationTestRunner
 *
 * To run just this suite from the command line:
 * $ adb shell am instrument -w \
 *   -e class com.example.android.apis.AllTests \
 *   com.example.android.apis.tests/android.test.InstrumentationTestRunner
 *
 * To run an individual test case, e.g. {@link com.example.android.apis.os.MorseCodeConverterTest}:
 * $ adb shell am instrument -w \
 *   -e class com.example.android.apis.os.MorseCodeConverterTest \
 *   com.example.android.apis.tests/android.test.InstrumentationTestRunner
 *
 * To run an individual test, e.g. {@link com.example.android.apis.os.MorseCodeConverterTest#testCharacterS()}:
 * $ adb shell am instrument -w \
 *   -e class com.example.android.apis.os.MorseCodeConverterTest#testCharacterS \
 *   com.example.android.apis.tests/android.test.InstrumentationTestRunner
 */
public class AllTests extends TestSuite {

    public static Test suite() {
        return new TestSuiteBuilder(AllTests.class)
                .includeAllPackagesUnderHere()
                .build();
    }
}

 

 


android 结合Junit进行测试._第1张图片

 

首先要卸载到原有的 demo 程序.

 

> adb uninstall com.example.android.apis

 

测试的过程就也需要重新安装下.

 

[2011-07-14 11:06:25 - ApiDemos] ------------------------------
[2011-07-14 11:06:25 - ApiDemos] Android Launch!
[2011-07-14 11:06:25 - ApiDemos] adb is running normally.
[2011-07-14 11:06:25 - ApiDemos] Warning: No instrumentation runner found for the launch, using android.test.InstrumentationTestRunner
[2011-07-14 11:06:25 - ApiDemos] Performing android.test.InstrumentationTestRunner JUnit launch
[2011-07-14 11:06:25 - ApiDemos] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'test_google'
[2011-07-14 11:06:25 - ApiDemos] WARNING: Application does not specify an API level requirement!
[2011-07-14 11:06:25 - ApiDemos] Device API version is 10 (Android 2.3.4)
[2011-07-14 11:06:25 - ApiDemos] Uploading ApiDemos.apk onto device 'emulator-5554'
[2011-07-14 11:06:33 - ApiDemos] Installing ApiDemos.apk...
[2011-07-14 11:06:42 - ApiDemos] Success!
[2011-07-14 11:06:43 - ApiDemos] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2011-07-14 11:06:43 - ApiDemos] Collecting test information
[2011-07-14 11:06:50 - ApiDemos] Sending test information to Eclipse
[2011-07-14 11:06:50 - ApiDemos] Running tests...
[2011-07-14 11:06:54 - ApiDemos] Attempting to connect debugger to 'com.example.android.apis' on port 8630
[2011-07-14 11:07:15 - ApiDemos] Test run finished
[2011-07-14 11:11:01 - ApiDemos] ------------------------------
[2011-07-14 11:11:01 - ApiDemos] Android Launch!
[2011-07-14 11:11:01 - ApiDemos] adb is running normally.
[2011-07-14 11:11:01 - ApiDemos] Performing android.test.InstrumentationTestRunner JUnit launch
[2011-07-14 11:11:01 - ApiDemos] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'test_google'
[2011-07-14 11:11:01 - ApiDemos] WARNING: Application does not specify an API level requirement!
[2011-07-14 11:11:01 - ApiDemos] Device API version is 10 (Android 2.3.4)
[2011-07-14 11:11:02 - ApiDemos] Application already deployed. No need to reinstall.
[2011-07-14 11:11:02 - ApiDemos] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2011-07-14 11:11:03 - ApiDemos] Collecting test information
[2011-07-14 11:11:05 - ApiDemos] Sending test information to Eclipse
[2011-07-14 11:11:05 - ApiDemos] Running tests...
[2011-07-14 11:11:08 - ApiDemos] Test run finished
[2011-07-14 11:11:33 - ApiDemos] ------------------------------
[2011-07-14 11:11:33 - ApiDemos] Android Launch!
[2011-07-14 11:11:33 - ApiDemos] adb is running normally.
[2011-07-14 11:11:33 - ApiDemos] Performing android.test.InstrumentationTestRunner JUnit launch
[2011-07-14 11:11:33 - ApiDemos] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'test_google'
[2011-07-14 11:11:33 - ApiDemos] WARNING: Application does not specify an API level requirement!
[2011-07-14 11:11:33 - ApiDemos] Device API version is 10 (Android 2.3.4)
[2011-07-14 11:11:34 - ApiDemos] Application already deployed. No need to reinstall.
[2011-07-14 11:11:34 - ApiDemos] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2011-07-14 11:11:35 - ApiDemos] Collecting test information
[2011-07-14 11:11:37 - ApiDemos] Sending test information to Eclipse
[2011-07-14 11:11:37 - ApiDemos] Running tests...
[2011-07-14 11:11:39 - ApiDemos] Test run finished

 测试结果如下:

 


android 结合Junit进行测试._第2张图片

 

啥也没有修改..还是有几个错误的..

 

总之很方便. 附件是代码..也可以自己从网站下.android-7 版本的.

你可能感兴趣的:(android 结合Junit进行测试.)