Android Test Tutorial (6): Test activity

Source: Internet
Author: User

Activity testing relies heavily on the Android Instrumation framework, and unlike other Android components, activity has complex lifecycle callback functions (such as OnCreate, OnStart, etc.) These callback functions are typically not called directly except through the Instrumation interface.

The basic test class for the test activity is instrumentationtestcase, which provides the instrumentation interface to the TestCase subclass. To support activity testing, Instrumentationtestcase provides the following features:

Lifecycle control: Using instrumentation, you can start, pause, and abort the activity being tested.

Dependency Injection:instrumentation allows you to create mock objects such as context,application to help test the activity, which helps you control the test environment and isolate it from the rest of the actual application. You can also customize some intent to start the activity.

User interface Interaction: You can use instrumentation to send keystrokes and touch events to the UI.

The following are the main test classes that are used to test the activity derived from TestCase:

ACTIVITYINSTRUMENTATIONTESTCASE2 is typically used for functional testing of multiple activity, which uses the normal system framework to run the operations (using the application itself) and uses the normal system context ( Non-mock) to test the function of an activity. Allows you to create some mock Intent to test the response of an activity. It is important to note that this testcase does not allow you to use mock context and Application object tests, which means you must test with the environment in which the application actually runs.

Activityunittestcase are often used to test individual activity. Before starting the activity being tested, you can inject a fake context or application to run the tested activity in an isolated environment in the mock contexts. Typically used for unit testing of activity, rather than interacting with anroid systems.

Singlelaunchactivitytestcase is used to test a single activity, and unlike Activityunittestcase, it only runs setup and teardown once rather than running testcase The setup and teardown are run before and after each test method in, which guarantees that the fixture will not be reset between running multiple tests, which can be used to test some associated methods.

This and the following are the basic concepts and related classes for activity,service,content Provider testing, followed by the use of these classes in conjunction with Apidemo->tests.

See a full set of tutorials: http://www.bianceng.cn/OS/extra/201301/35252.htm

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.