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();
    }
}

 

 

 

首先要卸载到原有的 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-7 版本的.

时间: 2024-09-20 00:02:10

android 结合Junit进行测试.的相关文章

【黑马Android】(02)短信发送器/布局演示/android下单位/android下Junit/保存数据/android下权限/xml解析和序列化

短信发送器 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima28.smssender" android:versionCode="1" android:versionName=&quo

logcat- 当运行Android的JUnit测试时,LogCat&amp;amp;#39;s Log.x() 在哪里输出?

问题描述 当运行Android的JUnit测试时,LogCat's Log.x() 在哪里输出? 当我通过测试TestCase和AndroidTestCase中的派生类来测试普通的java类时,LogCat输出消失. 那它是否仍然可以捕获这些输出信息?或者说我只能使用更缓慢的ActivityInstrumentationTestCase2 < >作为基类? 解决方案 我在 Android 2.1中也遇见相似的问题.Eclipse中可查看logcat,但在Android Junit模式中运行程序

Android的Junit测试

1.测试类要继承AndroidTestCase,然后写测试方法. 2.在AndroidManifest.xml清单文件中配置Junit测试的配置信息 <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.xxc.junit" /><!-- 写测试的包名 --> <uses-library andr

Android下junit单元测试、logCat的使用

Android下junit单元测试 软件测试小知识: 根据测试是否知道源代码:黑盒测试:只关心程序执行的过程和结果白盒测试:根据源代码写测试方法或者测试用例. 根据测试的粒度: 方法测试:function test单元测试:unit test 集成测试:intergration test 根据测试的次数: 冒烟测试:smoke test(android 猴子) 压力测试:prssure testAndroid单元测试: 1.Android测试类要继承AndroidTestCase类 2.写测试方

Android开发2——创建测试项目

一.创建普通Android项目   二.在AndroidManifest.xml添加两个配置 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.xy.app" android:versionCode="1&quo

junit 如何测试一个方法中的每行代码是否都被执行过?

问题描述 junit 如何测试一个方法中的每行代码是否都被执行过? 如题 测试一个方法中的每行代码是否都被执行过?是否有多余的代码? 解决方案 要知道一个代码块里面的代码是否被执行过,其实可以在该代码块最后加个输出语句即可,如果输出语句有执行,那么 前面的代码必然有执行. 但执行效果不一定是你的预期效果罢了(因为代码不一定是符合你的设计需求的) 解决方案二: 是否有多余的代码? 这个应该有代码校验工具. 解决方案三: 你这说的是白盒测试中的覆盖率测试,如果代码中有分支语句,需要编写多个测试用例才

junit 如何测试post请求 、去请求servlet??

问题描述 junit 如何测试post请求 .去请求servlet?? junit 如何测试post请求 .去请求servlet?? 解决方案 http://wenku.baidu.com/link?url=m-YXnEMRvzdrj4o0fP1xvq1vpl2DqwN6TFUX68-HlHs_nofT0XZI4Z4CN7xNFS5DCLRu3ixR8g0hSSTN5Msf2XpD5irF2gwBIviMQ6pAPoC

junit参数化测试的使用方法

  JUnit参数化测试的五个步骤:(1)为准备使用参数化测试的测试类指定特殊的运行器 org.junit.runners.Parameterized.(2)为测试类声明几个变量,分别用于存放期望值和测试所用数据.(3)为测试类声明一个带有参数的公共构造函数,并在其中为第二个环节中声明的几个变量赋值.(4)为测试类声明一个使用注解 org.junit.runners.Parameterized.Parameters 修饰的,返回值为 java.util.Collection 的公共静态方法,并在

cts-Linux系统 android 4.2 CTS测试

问题描述 Linux系统 android 4.2 CTS测试 如何屏蔽平板cts测试的短信和电话功能,因为我的机台是没有短信和电话功能的,但是直接跑cts包会有电话和短信的功能测项[X] android.hardware.telephony [_] android.hardware.telephony.cdma [X] android.hardware.telephony.gsm [_] android.hardware.type.television 这些测项还是enable的,如何屏蔽掉这些