The test application demonstrates these key points:

  • An Android test is itself an Android application that is linked to the application under test by entries in its AndroidManifest.xml file.
  • //一个Android的测试本身是由被测项目在AndroidManifest.xml文件链接到应用程序的Android应用程序, 这句话说明了两个问题: 测试本身也是安卓的一个应用;
  • Instead of Android components, an Android test application contains one or more test cases. Each of these is a separate class definition.
  • //一个安卓的测试应用包括一个或多个测试集合用来代替安卓的模块.每一个都是一个独立的类定义.
  • Android test case classes extend the JUnit TestCase class.
  • //安卓的测试集合继承自JUnit TestCase类.
  • Android test case classes for activities extend JUnit and also connect you to the application under test with instrumentation. You can send keystroke or touch events directly to the UI.
  • //对于activities的测试类继承自JUnit并且也能使你在测试集下使用instrumentation.你可以直接操作UI通过输入和触碰事件.
  • You choose an Android test case class based on the type of component (application, activity, content provider, or service) you are testing.
  • //你可以选择在一个组件(application, activity, content provider, or service)的基础上进行你的测试.
  • Additional test tools in Eclipse/ADT provide integrated support for creating test applications, running them, and viewing the results.
  • //除此之外, 在Eclipse/ADT 的工具能够支持创建测试应用, 跑测试和查看结果的集成套件

The test application contains methods that perform the following tests:

  • Initial conditions test. Tests that the application under test initializes correctly. This is also a unit test of the application's onCreate() method. Testing initial conditions also provides a confidence measure for subsequent tests.
  • //初始条件化的测试.在测试初始化正确下测试应用. 这也是一个应用的onCreate() 方法的单元测试.测试初始条件也包括了一个在随后的测试中提供一个可行的度量.
  • UI test. Tests that the main UI operation works correctly. This test demonstrates the instrumentation features available in activity testing. It shows that you can automate UI tests by sending key events from the test application to the main application.
  • //UI测试.测试那些能够被正确操作的主要UI.这个测试主要在activity测试中展示instrumentation特性的可用性. 它展示了你能够通过从测试应用发送主要事件到主要应用的方式自动化UI测试.
  • State management tests. Test the application's code for saving state. This test demonstrates the instrumentation features of the test runner, which are available for testing any component.
  • 申明管理测试集. 为了保存状态而测试应用的代码.这个测试展示了test runner的instrumentation的功能, 它能够对于测试任何组件可用.

The next step is to define the test case class. In this tutorial, you'll be creating a test case class that includes:

  • Test setup. This use of the JUnit setUp() method demonstrates some of the tasks you might perform before running an Android test.
    • getActivity(). Gets a reference to the activity under test (SpinnerActivity). This call also starts the activity if it is not already running.
    • findViewById(int). Gets a reference to the Spinner widget of the application under test.
    • getAdapter(). Gets a reference to the adapter (an array of strings) backing the spinner.

Add this code to the definition of SpinnerActivityTest, after the constructor definition:

  @Override
  protected void setUp() throws Exception {
    super.setUp();     setActivityInitialTouchMode(false);     mActivity = getActivity();     mSpinner =
      (Spinner) mActivity.findViewById(
        com.android.example.spinner.R.id.Spinner01
      );       mPlanetData = mSpinner.getAdapter();   } // end of setUp() method definition
  • Testing initial conditions. This test demonstrates a good testing technique. It also demonstrates that with Android instrumentation you can look at the application under test before the main activity starts. The test checks that the application's important objects have been initialized. If the test fails, you then know that any other tests against the application are unreliable, since the application was running in an incorrect state.

    Note: The purpose of testing initial conditions is not the same as using setUp(). The JUnit setUp() runs once beforeeach test method, and its purpose is to create a clean test environment. The initial conditions test runs once, and its purpose is to verify that the application under test is ready to be tested.

The initial conditions test verifies that the application under test is initialized correctly. It is an illustration of the types of tests you can run, so it is not comprehensive. It verifies the following:

  • The item select listener is initialized. This listener is called when a selection is made from the spinner.
  • The adapter that provides values to the spinner is initialized.
  • The adapter contains the right number of entries.

The actual initialization of the application under test is done in setUp(), which the test runner calls automatically before every test. The verifications are done with JUnit Assert calls. As a useful convention, the method name istestPreConditions():

  public void testPreConditions() {
    assertTrue(mSpinner.getOnItemSelectedListener() != null);
    assertTrue(mPlanetData != null);
    assertEquals(mPlanetData.getCount(),ADAPTER_COUNT);
  } // end of testPreConditions() method definition

Add this member:

  public static final int ADAPTER_COUNT = 9;
  • Testing the UI. This test shows how to control the main application's UI with instrumentation, a powerful automation feature of Android testing.
  • Testing state management. This test shows some techniques for testing how well the application maintains state in the Android environment. Remember that to provide a satisfactory user experience, your application must never lose its current state, even if it's interrupted by a phone call or destroyed because of memory constraints. The Android activity lifecycle provides ways to maintain state, and the SpinnerActivity application uses them. The test shows the techniques for verifying that they work.

Android Testing Point的更多相关文章

  1. Android Testing学习02 HelloTesting 项目建立与执行

    Android Testing学习02 HelloTesting 项目建立与执行 Android测试,分为待测试的项目和测试项目,这两个项目会生成两个独立的apk,但是内部,它们会共享同一个进程. 下 ...

  2. Android Testing学习01 介绍 测试测什么 测试的类型

    Android Testing学习01 介绍 测试测什么 测试的类型 Android 测试 测什么 1.Activity的生命周期事件 应该测试Activity的生命周期事件处理. 如果你的Activ ...

  3. 【Android Api 翻译2】Android Testing(1) 浅尝Android测试的奥秘

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 仅供学习和交流使用,翻译不好勿喷,请只摘除不合适的地方 Testing The Android fram ...

  4. Android testing tools

    引言 发现一篇关于android 测试的培训,英文的,很全面. Android Testing Training: http://www.vogella.com/training/android/an ...

  5. Android Testing(1) 浅尝Android测试的奥秘

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 仅供学习和交流使用,翻译不好勿喷,请只摘除不合适的地方 Testing The Android fram ...

  6. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.2——使用Android Testing Support Library进行测试

    问题: 你想要测试app的Android组件. 解决方案: 使用新的测试类实现JUnit风格的测试. 讨论: 测试像activities,services等的Android组件,需要将app部署到连接 ...

  7. 使用macaca抓页面元素,执行命令后报安装失败处理Error: Command failed: ……pm install -r "/data/local/tmp/com.macaca.android.testing"

    最近换了小米手机做自动化测试,执行命令的时候报安装失败错误,错误如下 解决:设置小米允许USB安装就好了 pm install -r "/data/local/tmp/com.macaca. ...

  8. [Android]官网《Testing Support Library》中文翻译

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5048524.html 翻译自 Android Develope ...

  9. 转一篇老外写的博文:Android automated testing (Robotium)

    Robotium的中文资料甚少,只得求助于老外,发现了一篇不错的文章:https://blog.codecentric.de/en/2011/03/android-automated-testing- ...

随机推荐

  1. Redis密码设置与访问限制(网络安全)

    现在用redis缓存热数据越来越常见了,甚至一些配置,开关等等的东西也写到redis里.原因就是redis简单高效.redis里的数据也越来越重要了,例如一些业务的中间数据会暂时存放在redis里,所 ...

  2. JavaScript前端和Java后端的AES加密和解密

    在实际开发项目中,有些数据在前后端的传输过程中需要进行加密,那就需要保证前端和后端的加解密需要统一.这里给大家简单演示AES在JavaScript前端和Java后端是如何实现加密和解密的. 直接上代码 ...

  3. Servlet实现后台分页查询

    相信大家在搭建后台的时候,经常会使用到分页功能,当然,目前有不少框架(如esayUI)都自带分页的实现,为了更好的理解分页原理,近期本人自己摸索了关于分页查询的一些心得. 归根结底,分页的核心还是在封 ...

  4. Django——模板层(template)(模板语法、自定义模板过滤器及标签、模板继承)

    前言:当我们想在页面上给客户端返回一个当前时间,一些初学者可能会很自然的想到用占位符,字符串拼接来达到我们想要的效果,但是这样做会有一个问题,HTML被直接硬编码在 Python代码之中. 1 2 3 ...

  5. git 一口气带你走完git之旅

    1.git是目前世界上最先进的分布式版本控制系统.svn是集成式版本控制系统,那么问题来了,什么叫分布式管理和集中式管理? 首先,svn 需要有一个中央服务器,协同开发者需要同中央服务器连接,所有的版 ...

  6. VirtualBox虚拟磁盘文件.vdi从C盘转移到D盘

    起因很简单:C盘快爆满了,用PS修个图的都无法保存.用Everything搜索了下C盘,发现有个WIN7的.vdi文件,竟然占据了15G的宝贵C盘空间!记得有一次,我直接把.vdi文件转移,然后在Vi ...

  7. react-native绑定优酷SDK播放视频-附效果和git源码

    ReactNative绑定优酷SDK需要用到两部分知识: 优酷本身的sdk绑定: RN与原生界面的交互: 效果: RN版本:0.49.3 代码更新日期:2017.10.26 下文也根据绑定优酷需要的两 ...

  8. MapReduce-实践1

      MR进阶实践1:  -file 分发多个文件 [-file 适合场景]分发文件在本地,小文件 -file分发原理         run.sh文件: 通过多个-file, 将多个本地文件分发到Ha ...

  9. JavaAPI 中 <E> 与 <T> 的含义

    今天看集合的代码,发现在泛型的使用时的区别,Collection<E>.List<E>,而Iterator<T>,那么<E>和<T>含义有什 ...

  10. JavaWeb之Eclipse中使用Maven构建SpringMVC项目

    为了学习spring和maven我这也是拼了老命了,光使用maven配置springmvc我花了上周一周的时间,下班回来就搞,一直有bug,一个bug接着一个,昨天一整天都在解决配置的问题,让大学同学 ...