Android Testing Point

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.
时间: 2024-11-06 23:09:23

Android Testing Point的相关文章

Android testing tools

引言 发现一篇关于android 测试的培训,英文的,很全面. Android Testing Training: http://www.vogella.com/training/android/androidtesting.html 正文 1.AppiumAppium 是一个开源.跨平台的自动化测试工具,用于测试原生和轻量移动应用,支持 iOS, Android 和 FirefoxOS 平台.Appium 驱动苹果的 UIAutomation 库和 Android 的 UiAutomator

Android官方训练课程翻译 Android Testing

Best Practices for Testing Android测试最佳实践 Testing your app is an integral part of the app development process. Testing allows you to verify the correctness, functional behavior, and usability of your app before it is released publicly. 测试你的应用是开发过程中必不可

Android Testing Support Library

This release includes three major updates:Espresso-Web[1]: an Espresso-like API for automated UI testing of hybrid Android applications. Use Espresso-Web to test and interact with any WebView and seamlessly integrate it with your native Espresso UI t

5 Best Automation Tools for Testing Android Applications

Posted In | Automation Testing, Mobile Testing, Software Testing Tools Nowadays automated tests are used during almost every testing process. This is not surprising, as properly organized automated testing greatly reduces time needed for a testing pr

Android application testing with the Android test framework

目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional tests 3 JUnit 3 4 Running tests on a server without display Test hooks into the Android framework 1 Instrumentation 2 How the Android system executes

拥抱 Android Studio 之五:Gradle 插件开发

实践出真知 笔者有位朋友,每次新学一门语言,都会用来写一个贪吃蛇游戏,以此来检验自己学习的成果.笔者也有类似体会.所谓纸上得来终觉浅,绝知此事要躬行.这一章,笔者将以开发和发布一个 Gradle 插件作为目标,加深学习成果. 官方文档给出了比较详细的实现步骤,本文的脉络会跟官方文档差不了太多,额外增补实际例子和一些实践经验.文中的代码已经托管到了 github 项目中. 需求 默认的 Android 打包插件会把 apk 命名成 module-productFlavor-buildType.ap

Android UnitTest FrameWork

Android test suites基于Junit,可以直接使用Junit测试不使用android api的class,也可以使用android的Junit extensions测试android component android的Junit extensions提供了基于component的测试类,这些类可以帮助创建mock 对象,控制componetn的生命周期 Android testing apid支持Junit3的code style,而不是Junit4 Android instr

Android软硬整合设计与框架揭秘: HAL&Framework &Native Service &App&HTML5架构设计与实战开发

掌握Android从底层开发到框架整合技术到上层App开发及HTML5的全部技术: 一次彻底的Android架构.思想和实战技术的洗礼: 彻底掌握Andorid HAL.Android Runtime.Android Framework.Android Native Service.Android Binder.Android App.Android Testing.HTML5技术的源泉和精髓等核心技术,不仅仅是技术和代码本身,更重要的是背后的设计思想和商业哲学. 一.课程特色 l  贯通And

Android测试:从零开始2——local单元测试

上一篇分析了android项目的测试分类,这一篇讲local单元测试. 参考android官方文档. 测试前需要配置测试环境,新建项目后,目录下会出现app/src/test/java/文件夹,这个文件夹是用于存放local单元测试代码的(就是与androidAPI无关的测试). 在build.gradle配置文件中增加测试依赖: dependencies { // Required -- JUnit 4 framework testCompile 'junit:junit:4.12' // O