Android Unit Test

来源:互联网 发布:淘宝网网页版登录入口 编辑:程序博客网 时间:2024/05/28 15:24

Android Unit Test

测试框架

  • 测试基础
    概念:
    这里写图片描述

    1. 继承JUnit,
    2. 通过测试包组织测试用例,
    3. 使用InstrumentationTestRunner或其子类运行测试,
    4. 使用Android Tool自动构建测试项目,
    5. 组件测试+特殊测试
      测试框架结构:
      结构
      测试包:
      这里写图片描述
      组件测试:

    With Android instrumentation, though, you can invoke callback methods in your test code. This allows you to run through the lifecycle of a component step by step, as if you were debugging the component. The following test code snippet demonstrates how to use this to test that an Activity saves and restores its state.

  • // Start the main activity of the application under test    mActivity = getActivity();// Get a handle to the Activity object's main UI widget, a SpinnermSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01);// Set the Spinner to a known positionmActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);// Stop the activity - The onDestroy() method should save the state of the SpinnermActivity.finish();// Re-start the Activity - the onResume() method should restore the state of the SpinnermActivity = getActivity();// Get the Spinner's current positionint currentPosition = mActivity.getSpinnerPosition();// Assert that the current position is the same as the starting positionassertEquals(TEST_STATE_DESTROY_POSITION, currentPosition);

    AndroidTestCase

    • Provider
    • Application
    • Service

    InstrumentationTestCase

    • ActivityTestCase
      • TestCase2
      • UnitTest

    Assert

    • MoreAsserts
    • ViewAsserts

    配置测试执行器:testInstrumentationRunner(gradle or manifest)
    测试包的命名和测试类的命名

    Activity测试
    ActivityInstrumentationTestCase2

    • 不支持Mock Context以及Mock Application
    • 支持Mock Intents (call setActivityIntent before getActivity)
    • 可以自动创建Activity
    • 不能和系统独立

    ActivityUnitTestCase

    • 支持Mock Context以及Mock Application
    • 能和系统独立
    • 不可以自动创建Activity
    • 不支持Mock Intents
      测神马?
    • View的状态
    • 事件(系统事件&用户事件)
    • 对Intent的响应
    • 配置的改变
    • 屏幕大小和分辨率

    Tips on ui thread test

    • @UiThreadTest 方法级,问题
    • appActivity.runOnUiThread() 块级
    • 一些方法只能在测试线程中调用,不能再UI线程调用
    • 在setup中Turning off touch mode
    • ActivityInstrumentationTestCase2.setActivityTouchMode(false)
    • 禁用锁屏
      manifest:<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
      activities:
        mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);  mLock = mKeyGuardManager.newKeyguardLock("activity_classname");  mLock.disableKeyguard();

    Service测试:
    主角:ServiceTestCase

    • 设置 setApplication,setContext(mock)
    • 启动 startService,bindService
    • mock application,extends MockApplication
    • mock Context IsolatedContext RenamingDelegatingContext

    测神马?

    • 在某个action下,生命周期方法是否被正确的调用
    • 测试Service实现的业务逻辑

    ContentProvider测试
    重点是独立测试环境
    主角:ProviderTestCase2

    • 在构造器中初始化Mock Object:IsolatedContext&MockContentResolver
    • 返回ContentProvider
    • 与系统相隔离的测试环境

    测神马?

    • ContentResolver
    • Provider中暴露的常量
    • 测试增删改查
    • 测试业务逻辑
  • 第三方
    uiautomator-自动化UI测试工具。

    <未完待续>

0 0