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