Android Test Tutorial (4): Testing the relevant API

Source: Internet
Author: User
Tags assert

The Android test framework-related APIs are mainly defined in three packages:

Android.test used to write Android test cases

Android.test.mock defines a test "pile" for convenience testing

Android.test.suitebuilder Test Runner class to run

The Android Test API is based on JUnit extensions and adds the test API associated with the Android platform.

Junit

You can write some platform-independent test cases (based on testcase) directly using the relevant APIs in JUnit, and the Android Test API provides a testcase subclass Androidtestcase. A test case that can be used to write some Android-related objects, Androidtestcase supports some platform-related Setup,teardown and Setup methods.

You can also display test results directly using the JUnit Assert method, which allows you to exclude exceptions by comparing expected values and actual values, if different. The Android Test API extends some assert methods to support comparisons with the Android platform.

Note that the Android Test API supports the JUnit 3 code style, not the JUnit 4 code style, and only uses Instrumentationtestrunner to run test cases.

Instrumentation

Android's instrumentation offers some "hooks" to connect to the Android operating system, independently controlling the lifecycle of the Android component (Activity,service, etc.) and controlling how Android invokes an application.

Under normal circumstances (ordinary Android apps), Android's Activity,service and so on are the lifecycle of
The Android operating system to control. For example, the life cycle of an activity begins in OnCreate (activated by a intent), and then Onresume. See Android Simple development tutorial Five: activities. The application itself cannot directly control the transitions of these life cycle states. But you can call these methods directly when using the Instrumatation API.

The instrumentation API can also support forcing an application to run in the same process as another already operational application, which is not possible under normal circumstances.

You can use the instrumentation API to invoke the lifecycle callback function of an activity or service directly, allowing you to run a step-by-step run of activity or service life cycle functions. The following example shows how to use the instrumentation API to test activity retention and restore state.

//Start The main activity of the//application under Test mactivity = getactivity (); Get a handle to the Activity object ' s//main UI widget, a Spinner Mspinner = (Spinner) mactivity. findviewbyi     

D (Com.android.example.spinner.r.id.spinner01);     

Set the Spinner to a known position mactivity.setspinnerposition (test_state_destroy_position);     

Stop the Activity-the OnDestroy ()//method should save the state of the Spinner Mactivity.finish (); Re-start the activity-the onresume ()//method should restore the state of the Spinner mactivity = Getactivi     

Ty ();     

Get the Spinner ' s current position int currentposition = Mactivity.getspinnerposition (); Assert that the "position is" the//same as the starting position assertequals (test_state_destroy_posit ION, currentposition); 

One of the key methods is getactivity (), and only the activity that is tested after the call to Getactivity () is started. In addition, the instrumentation API allows you to run test projects and tested application projects into the same process, so that you can directly invoke the methods of the test application and access its members in the test code.

Test Case Related class

Android provides a number of subclasses derived from TestCase or assert to support the Android platform-related Setup,teardown and other helper methods.

Androidtestcase is a generic test class under an Android platform that supports all JUnit assert methods and standard setup and Teardown methods and can be used to test the Android permission.

Component-related test classes such as test activity, Content provider, Service-related test classes, Android does not provide separate test classes for testing Broadcastreceiver, Instead, you can test broadcastreceiver by sending intent objects to detect broadcast receiver responses.

Applicationtestcase can be used to test application objects.

Instrumentationtestcase If you want to use the instrumentation API, then you must use Instrumentationtestcase or its subclasses.

Assertion Classes

The Assert methods provided in JUnit can be used to display test results in the Android test. In addition, the testing API also provides moreasserts and Viewasserts classes. The Moreasserts supports more comparison methods, including Regex (regular) comparisons. Viewasserts can be used to validate UI View.

Mock Object Classes

The Android.test.mock package defines some test "pile" classes, such as Mockapplication, Mockcontentprovider, Mockcontext,mockcursor, Mockpackagmanager and other use cases help test.

The following is a detailed description of how to use these APIs to write test cases.

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.