Android Unit Test

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 Spinner
    mSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01);
    // Set the Spinner to a known position
    mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);
    // Stop the activity - The onDestroy() method should save the state of the Spinner
    mActivity.finish();
    // Re-start the Activity - the onResume() method should restore the state of the Spinner
    mActivity = getActivity();
    // Get the Spinner's current position
    int currentPosition = mActivity.getSpinnerPosition();
    // Assert that the current position is the same as the starting position
    assertEquals(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值