Android Testing Point

來源:互聯網
上載者:User

標籤:des   android   style   os   io   使用   ar   strong   for   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.