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 andro

学习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 版本的.

目录
相关文章
|
1月前
|
IDE Java 测试技术
Junit 单元测试
JUnit是Java常用的单元测试框架,简化了测试用例的编写和执行。其特点包括简单注解、自动化测试、可扩展性、灵活性及与IDE的集成。使用方法涉及创建测试类、利用注解如@Test、@BeforeEach等管理测试生命周期,以及使用各种断言方法验证结果。此外,JUnit支持参数化测试以覆盖更多输入组合,并能与Maven、Gradle等构建工具集成,提升测试效率和项目管理。
38 1
|
2月前
|
监控 安全 Android开发
【新手必读】Airtest测试Android手机常见的设置问题
【新手必读】Airtest测试Android手机常见的设置问题
|
3月前
|
Java 测试技术
JUnit 4 单元测试
单元测试是软件开发中必不可少的一环,但是在平常开发中往往因为项目周期紧,工作量大而被选择忽略,这样往往导致软件问题层出不穷。 线上出现的不少问题其实在有单元测试的情况下就可以及时发现和处理,因此培养自己在日常开发中写单元测试的能力是很有必要的。无论是对自己的编码能力的提高,还是项目质量的提升,都是大有好处,本文将介绍 Java 单元测试框架 JUnit 4 的基础认识和使用来编写单元测试,希望同样对你有所帮助。
|
15天前
|
Java 测试技术 程序员
junit单元测试
junit单元测试
|
3月前
|
SQL Java 数据库连接
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)
【1月更文挑战第2天】 MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁移到了Google Code。随着开发团队转投Google Code旗下,iBatis3.x正式更名为MyBatis。代码于2013年11月迁移到Github iBatis一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBatis提供的持久层框架包括SQL Maps和Data Access Objects(DAO)
202 3
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)
|
2天前
|
Java API Android开发
Android 11 修改libcore Cipher AS测试
Android 11 修改libcore Cipher AS测试
13 1
|
1月前
|
XML Java 测试技术
TestNG 与 JUnit 测试框架:哪个更好?
【2月更文挑战第16天】
46 1
TestNG 与 JUnit 测试框架:哪个更好?
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
34 3
|
1月前
|
存储 SQL 数据库
【Android 从入门到出门】第六章:使用Room数据库并测试
【Android 从入门到出门】第六章:使用Room数据库并测试
29 4
|
2月前
|
运维 Java 测试技术
spring 单元测试 Junit
spring 单元测试 Junit
43 2