Android program test (2) -- Activity Test

Source: Internet
Author: User

Compared with Content Provider testing and Service testing, Activity testing is particularly inseparable from the Android testing framework (instrumentation framework). This is because activities in Android have a complex life cycle, all stages of these lifecycles are based on callback methods, which can only be called directly by the Android testing framework. That is to say, in a program, the only way to send events to the UI is through the Android testing framework.

This document describes how to perform activities testing.

Before starting, it is necessary to introduce the classes related to activity testing:

InstrumentationtestcaseThis class is the base class for activities testing. It is mainly used to provide testing instrumentation for the subclass of the test case for activities testing. For activity testing, this base class provides the following functions:

Activity lifecycle control: With the instrumentation provided by InstrumentationTestCase, we can use the test case class to start, pause, and destroy the activity to be tested.

Dependency injection: Instrumentation allows us to create and use mock system objects such as Context and Application to run the activity to be tested. The biggest advantage of doing so is to control the test environment and isolate the activity to be tested from the product environment. In addition, you can write intent by yourself and start the activity through the intent.

UI interaction: Through instrumentation, we can directly send buttons and touch-screen events to the UI of the activity to be tested.

By inheriting TestCase and Assert, the activity test class also provides the JUnit test framework. The two main test subclasses are ActivityInstrumentationTestCase2 and ActivityUnitTestCase.

Activityinstrumentationtestcase2Is designed to test the functionality of one or more activities in a program. The tested activity runs in an independent test program instance using the standard system Context, and we can write the activity to send the simulated intent, however, we cannot simulate the Applications where Context or activity is located.

ActivityunittestcaseThis class is mainly used to test a single activity in an isolated environment. Before starting this activity, we can inject a simulated Context or Application, but we cannot send simulated intents to this tested activity.

MockapplicationIt is mainly used to control the test environment and can be used only when ActivityUnitTestCase is used as the test case class.

ViewassertsIs mainly used to define assertion)

The following describes the specific process of activity testing:

First, create an android project, which is used as the tested project, based on the example of a Spinner in the SDK installation directory. Create a new test project for testing the SpinnerActivity, named "SpinnerActivityTest", and add ". "test", select "SpinnerActivity" for the target of the test. For details about how to create a test project, see:

Http://blog.csdn.net/yaolingrui/article/details/7360101

Another way to create a test project is to create a test project at the same time when creating the project to be tested. This is a recommended creation process. For more information, see the relevant sections in the official documentation, official documentation:

Http://developer.android.com/resources/tutorials/testing/activity_test.html

After the test project is created, the basic framework for testing is set up in the test project. in the XML file, we will automatically add relevant test information, including the specified program to be tested, and use android. test. runner class library.

After creating a test project, create

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.