`
yanghaoli
  • 浏览: 292383 次
社区版块
存档分类
最新评论

android sdk 接口测试

 
阅读更多

最近接触到了第一个安卓项目,是关于安卓的接口测试的。于是呼一通找资料,看了五花八门的文章、文档,但是并没有非常适合的。

现在决定把学习的过程写下来,如果能帮到一两个跟我一样情况的就很开心了,开始学着记录学习笔记。

 Android提供了一系列强大的测试工具,它针对Android的环境,扩展了JUnit测试框架。允许你为应用程序的各个方面进行更为复杂的测试,包括单元层面及框架层面。

Android测试工具包含几个包:android.test, android.test.mock, android.test.suitebuilder, 这里面最重要的包是android.test。

我们来看android.test 的包里面的类的关系:

 

可以看出这里面的类分两个体系, 

androidTestCase 体系

继承自JUnit的TestCase,不能使用Instrumentation框架。但这些类包含访问系统对象(如Context)的方法。使用Context,你可以浏览资源,文件,数据库等等。基类是AndroidTestCase,一般常见的是它的子类,和特定组件关联。

子类有:

  • ApplicationTestCase——测试整个应用程序的类。它允许你注入一个模拟的Context到应用程序中,在应用程序启动之前初始化测试参数,并在应用程序结束之后销毁之前检查应用程序。
  • ProviderTestCase2——测试单个ContentProvider的类。因为它要求使用MockContentResolver,并注入一个IsolatedContext,因此Provider的测试是与OS孤立的。
  • ServiceTestCase——测试单个Service的类。你可以注入一个模拟的Context或模拟的Application(或者两者),或者让Android为你提供Context和MockApplication。

Instrumentation 体系

继承自JUnit TestCase类,并可以使用Instrumentation框架,用于测试Activity。使用Instrumentation,Android可以向程序发送事件来自动进行UI测试,并可以精确控制Activity的启动,监测Activity生命周期的状态。

基类是InstrumentationTestCase。它的所有子类都能发送按键或触摸事件给UI。子类还可以注入一个模拟的Intent。

子类有:

  •    ActivityTestCase——Activity测试类的基类。
  •    SingleLaunchActivityTestCase——测试单个Activity的类。它能触发一次setup()和tearDown(),而不是每个方法调用时都触发。如果你的测试方法都是针对同一个Activity的话,那就使用它吧。
  •    SyncBaseInstrumentation——测试Content Provider同步性的类。它使用Instrumentation在启动测试同步性之前取消已经存在的同步对象。
  •    ActivityUnitTestCase——对单个Activity进行单一测试的类。使用它,你可以注入模拟的Context或Application,或者两者。它用于对Activity进行单元测试。

不同于其它的Instrumentation类,这个测试类不能注入模拟的Intent。

  •    ActivityInstrumentationTestCase2——在正常的系统环境中测试单个Activity的类。你不能注入一个模拟的Context,但你可以注入一个模拟的Intent。另外,你还可以在UI线程(应用程序的主线程)运行测试方法,并且可以给应用程序UI发送按键及触摸事件。

可以看这两个的区别, androidTestCase的适用于与activity无关的测试, 如程序逻辑, instrumentation适用于acitivity的测试。

先引用下网上的例子:

1.首先建立一个Android project,类名为Sample,代码如下:

package com.hustophone.sample;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class Sample extends Activity {
    private TextView myText = null;
    private Button button = null;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myText = (TextView) findViewById(R.id.text1);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                myText.setText("Hello Android");
            }
        });
    }
 
    public int add(int i, int j) {
        return (i + j);
    }
}

这个程序的功能比较简单,点击按钮之后,TextView的内容由Hello变为Hello Android.同时,在这个类中,我还写了一个简单的add方法,没有被调用,仅供测试而已。

2. 在src文件夹中添加一个测试包,在测试包中添加一个测试类SampleTest

测试类的代码如下:

package com.hustophone.sample.test;
 
import com.hustophone.sample.R;
import com.hustophone.sample.Sample;
import android.content.Intent;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
 
public class SampleTest extends InstrumentationTestCase {
    private Sample sample = null;
    private Button button = null;
    private TextView text = null;
 
    /*
     * 初始设置
     * @see junit.framework.TestCase#setUp()
     */
    @Override
    protected void setUp()  {
        try {
            super.setUp();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Intent intent = new Intent();
        intent.setClassName("com.hustophone.sample", Sample.class.getName());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        sample = (Sample) getInstrumentation().startActivitySync(intent);
        text = (TextView) sample.findViewById(R.id.text1);
        button = (Button) sample.findViewById(R.id.button1);
    }
 
    /*
     * 垃圾清理与资源回收
     * @see android.test.InstrumentationTestCase#tearDown()
     */
    @Override
    protected void tearDown()  {
        sample.finish();
        try {
            super.tearDown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /*
     * 活动功能测试
     */
    public void testActivity() throws Exception {
        Log.v("testActivity", "test the Activity");
        SystemClock.sleep(1500);
        getInstrumentation().runOnMainSync(new PerformClick(button));
        SystemClock.sleep(3000);
        assertEquals("Hello Android", text.getText().toString());
    }
 
    /*
     * 模拟按钮点击的接口
     */
    private class PerformClick implements Runnable {
        Button btn;
        public PerformClick(Button button) {
            btn = button;
        }
 
        public void run() {
            btn.performClick();
        }
    }
 
    /*
     * 测试类中的方法
     */
    public void testAdd() throws Exception{
        String tag = "testAdd";
        Log.v(tag, "test the method");
        int test = sample.add(1, 1);
        assertEquals(2, test);
    }
}

下面来简单讲解一下代码:
setUp()和tearDown()都是受保护的方法,通过继承可以覆写这些方法。

在android Developer中有如下的解释
protected void setUp ()
Since: API Level 3
Sets up the fixture, for example, open a network connection. This method is called before a test is executed.

protected void tearDown ()
Since: API Level 3
Make sure all resources are cleaned up and garbage collected before moving on to the next test. Subclasses that override this method should make sure they call super.tearDown() at the end of the overriding method.

setUp ()用来初始设置,如启动一个Activity,初始化资源等。
tearDown ()则用来垃圾清理与资源回收。

在testActivity()这个测试方法中,我模拟了一个按钮点击事件,然后来判断程序是否按照预期的执行。在这里PerformClick这个方法继承了Runnable接口,通过新的线程来执行模拟事件,之所以这么做,是因为如果直接在UI线程中运行可能会阻滞UI线程。

2.要想正确地执行测试,还需要修改AndroidManifest.xml这个文件.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hustophone.sample" android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <!--用于引入测试库-->
        <uses-library android:name="android.test.runner" />
        <activity android:name=".Sample" 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>
 
    <uses-sdk android:minSdkVersion="3" />
    <!--表示被测试的目标包与instrumentation的名称。-->
    <instrumentation android:targetPackage="com.hustophone.sample" android:name="android.test.InstrumentationTestRunner" />
</manifest>

 

经过以上步骤,下面可以开始测试了。测试方法也有以下几种,下面介绍两个常用的方法:

(1) 用Eclipse集成的JUnit工具
在Eclipse中选择工程Sample,单击右键,在Run as子菜单选项中选择Android JUnit Test

同时可以通过LogCat工具查看信息

(2) 使用Intellij IDEA 或者Android studio

右键点击你的测试工程,或者测试类, 选择Run,然后选择机器人图标的那个选项,窗口和Eclipse的差不多,

使用LogCat查看信息。

 

1
0
分享到:
评论

相关推荐

    android sdk android-33

    Android SDK 是 Android 应用开发的核心工具集,它包含了开发者构建、测试和部署 Android 应用所需的所有组件。"android-33" 指的是 Android SDK 的一个特定版本,代表了 Android 操作系统的第 33 版本。这个版本...

    Android SDK (SDK Platforms)-android-34-ext10.zip

    Android SDK(软件开发工具包)是Google为Android平台的开发者提供的开发环境,包含了开发Android应用程序所需的各种工具、库文件以及API接口。SDK Platforms是其中的一个组件,它为开发者提供了特定版本Android平台...

    Android SDK (SDK Platforms)-android-28.zip

    Android SDK是Android开发者必须拥有的工具集,它提供了开发、测试和调试Android应用程序所需的所有组件。在本文中,我们将深入探讨Android SDK中的关键组成部分以及它们在Android开发过程中的作用。 首先,我们要...

    Android SDK (SDK Platforms)-android-32.zip

    总之,Android SDK (SDK Platforms)-android-32.zip是Android开发者构建和测试针对Android 13应用的重要资源,包含了所有必要的组件和文档,使得开发者能够充分利用新版本的功能,并确保应用的兼容性和性能。

    Android SDK (SDK Platforms)-android-24.zip

    《Android SDK (SDK Platforms) - android-24详解》 Android SDK(Software Development Kit)是开发者构建、调试和发布Android应用程序的重要工具集。在本文中,我们将深入探讨Android SDK中的"SDK Platforms"针对...

    Android SDK (SDK Platforms)-android-31.zip

    11. **测试与调试**:SDK Platforms中的工具集可以帮助开发者进行单元测试、兼容性测试、性能测试等,以确保应用在各种设备和Android版本上都能正常工作。 12. **发布与分发**:Android SDK也包含用于打包和签名...

    Android SDK (SDK Platforms)-android-34.zip

    总之,Android SDK (SDK Platforms)-android-34.zip是开发者进入Android 13开发世界的入口,提供了一整套工具来创建、测试和发布兼容新系统的应用。理解和掌握其中的知识点是开发高质量Android应用的关键步骤。

    Android SDK (SDK Platforms)-android-25.zip

    《Android SDK (SDK Platforms) - android-25详解》 Android SDK(Software Development Kit)是开发者构建、调试和发布Android应用程序的重要工具集。在本文中,我们将深入探讨Android SDK中的"SDK Platforms"-...

    Android veridex 非SDK接口检测工具

    3. 跟踪并测试非SDK接口的稳定性,确保在新的Android版本下仍能正常工作。 4. 尽量依赖于官方的Android Support Library或AndroidX库,这些库提供了很多兼容性解决方案。 **总结** Veridex是Android开发者必备的...

    Android SDK (SDK Platforms)-android-27.zip

    9. **测试框架**:JUnit和 Espresso是Android SDK中用于单元测试和UI测试的框架。开发者可以利用它们来自动化测试,确保应用的质量。 10. **Gradle插件**:Android项目通常使用Gradle作为构建系统,Gradle插件与SDK...

    Android SDK (SDK Platforms)-android-30.zip

    1. **Android SDK Platform**: 这是SDK的核心部分,包含了Android系统框架的二进制文件,包括系统库和API接口。开发者可以使用这些文件来了解和访问Android API,编写与API级别30兼容的应用。 2. **API级别30**: ...

    Android SDK Platform 28

    这些功能通过Android的编程接口暴露给开发者,使得构建应用程序时能够充分利用Android平台的能力。 随着技术的不断进步,Android SDK Platform也在持续更新。每一个新版本都旨在解决开发过程中遇到的问题,提高应用...

    Android SDK (SDK Platforms)-android-29.zip

    3. **框架接口**:Android 29的SDK包含了所有必要的框架接口,让开发者可以访问系统服务,如ActivityManager、ContentResolver等,以实现各种功能。 4. **权限管理系统**:Android Q进一步强化了权限管理,例如,...

    Android SDK (SDK Platforms)-android-20.zip

    Android SDK(Software Development Kit)是开发者构建、测试和调试Android应用程序所需的核心工具集。在Android SDK中,SDK Platforms扮演着至关重要的角色,它包含了特定Android版本的系统库、API文档以及必要的...

    Android SDK (SDK Platforms)-android-26.zip

    API Level 26对应的是Android 8.0,包含了系统的类库、API接口和系统服务,开发者可以调用这些接口来实现应用功能。 3. **Android Oreo (8.0)**:这是Android操作系统的第八个主要版本,发布于2017年。它的主要特性...

    Android SDK (SDK Platforms)-android-22.zip

    1. **Android SDK**:全称为Android Software Development Kit,是Google为开发者提供的一个工具集,用于创建、测试和调试Android应用程序。它包括各种工具、库、文档和模拟器,帮助开发者完成整个开发流程。 2. **...

    android sdk21资源包

    Android SDK21资源包是Android开发中的一个重要组成部分,它包含了Android 5.0 Lollipop版本的所有开发者工具和库。这个资源包是为开发者提供构建、测试和调试针对Android 5.0系统的应用所必需的。下面我们将深入...

    Android SDK (SDK Platforms)-android-33-ext5.zip

    在解压"Android SDK (SDK Platforms)-android-33-ext5.zip"后,开发者会得到关于Android 33 API的详细文档,包括API级别、类库、接口以及方法等。这些文档可以帮助开发者理解新版本的API如何使用,以及如何与旧版本...

    Android sdk 24版本

    Android SDK 24版本是Android操作系统的一个重要更新,主要针对开发者提供了一系列的工具和技术支持,以便构建、测试和优化在Android 7.0 (Nougat)系统上运行的应用程序。这一版本的SDK包含了对新功能、性能优化以及...

    android sdk platforms 29

    Android SDK Platforms 29是Android开发中的一个重要组成部分,它包含了Android操作系统的API等级29(也称为Android Q)的所有框架接口、系统库和服务。这个版本的SDK为开发者提供了构建、测试和调试针对Android 10...

Global site tag (gtag.js) - Google Analytics