Android testing framework

Source: Internet
Author: User
This article mainly introduces Features of the android testing framework, Contains the Test Structure, API used to create a test, Tools used to run tests and view results. This article may not be a hard translation SDK. It is just used to give a rough introduction to the android testing framework. The specific sample code is being compiled and recorded on the blog next month. I. Test Framework featuresAs an important part of the android development environment, the android testing framework can be used to test all aspects of the application, from unit testing to framework testing. This testing framework has the following features: 1, Based on JUnit: The Android test suite is based on JUnit 3 (so it is not fully compatible with JUnit 4). Therefore, you can use JUnit without calling the android test API for testing, of course, we recommend that you use Android testing APIs for more efficient and comprehensive testing. 2, Component Testing: The JUnit extension of Android provides test case classes for components. These classes provide auxiliary methods for mock objects and methods to control the life cycle of components. 3, The test project is similar to the application project:The test case class is included in a test package similar to the main package, so you do not need to learn other technologies and tools. 4, Automatically create a test project: The SDK tool used to automatically build and run tests is available in eclipse ADT, and can be completed using commands in other ides. They can read the information of the Source Project and automatically create the test class. 5, Automatic event stream simulation toolMonkeyrunner and monkey: they can be used for UI testing. Both of them can send an event stream (keyboard, touch, and gesture) to the device to perform stress testing on the program, here, monkeyrunner needs to write a python program to call the API to send the event stream, and monkey is sent through the ADB command line. For more details, see section 7. Ii. Test Framework StructureThe following is the structure diagram of the test framework: we have found the following points. The test package and application package run in the same process.And interact with each other through instrumentationtestrunner. In general, applications run in their own independent Dalvik processes, and the test package and application package are two projects. How do they run in the same process? What is the role of instrumentationtestrunner? These will be explained later. B. Both the test tool and monkey and monkeyrunner run outside the process.. It also interacts with each other through instrumentationtestrunner. C. The test package consists of test case classed and mock objects. Iii. Android test case structureThe android test case structure is similar to that of JUnit. The significant difference is that the test cases are managed separately in a project and can be automated (IDE plug-in or command line) establish a test project. Refer: Iv. APIs used for testing4.1 junitjunit3 APIs can be used in Android. Android androidtestcase is also a testcase inherited from JUnit. Similar to assert 4.2 Instrumentation Instrumentation is a collection of control functions and hooks in the Android system.. These hooks make the android control independent of its normal lifecycle and how the system loads programs. In other words, Android itself opens a backdoor for itself .. A. Explicitly controls the lifecycle of system components.Generally, the life cycle of a component is controlled by the system. For example, functions such as oncreate, onresume, and onstop of a familiar activity life cycle are called by the system, in addition, the Android Application Framework does not provide permissions for users to directly call, but can be called in instrumentation. B. The test and program are running in the same process.All components of the Android Application (except content providers) run in the same Dalvik process and cannot run the same application in the same process. However, instrumentation allows the test program and the corresponding application to run in the same process, so that you can easily control the component lifecycle and access data. Now we know the first question in the above structure diagram. 4.3 Test Cases4.3.1 androidtestcase inherits from testcase and features similar to the testcase of JUnit. It contains the classic setup and teardown functions. It also provides test permissions and methods to prevent memory leakage by removing class references. 4.3.2 Component-specific test cases component-oriented test cases. Including activity testing, content provider testing, and service testing. Broadcastreceiver is not provided separately because it can be tested by passing intent. 4.3.3 applicationtestcase use applicationtestcase to test the setup and teardown of the Application object. These objects maintain the global state of the application. These test cases can verify the configuration of the application element in the manifest file. 4.3.4 if you want to use the instrumentation method in a test case, you must use instrumentationtestcase or its subclass. For example, the test case of activity inherits from instrumentationtestcase, other functions used in activity testing are also extended. 4.4 Assertion classesLike JUnit testing, assert can also be used in Android testing to display test results. In addition to the available assert class of JUnit, the android test API also provides moreasserts and viewasserts. Where MoreAssertsIt provides more powerful assert functions, such as regular expression comparison, sequence comparison, and equal judgment of richer objects. ViewAssertsProvides the view (UI) geometric and alignment test. Such as location, view, and include judgment. 4.5 mock object classesDependency injection can be easily tested. Android provides classes for system objects such as mock context, contentprovider, contentresolver, and service, and some also provide mock for intent objects. You can use these mocks to separate a test point or inject dependencies for testing.. These classes can be found in Android. Test and Android. Test. Mock packages. Android provides two types of mock objects: Class. simple mock object classes these classes provide a simple mock policy. They abolish the corresponding system objects and throw an exception if the corresponding system objects are called. The mock dependency is achieved through rewriting. Including MockApplication, MockContext, MockContentProvider, MockCursor, MockDialogInterface, MockPackageManager, And MockResources. B. resolver mock objects separates the data provider from the test by providing an independent parser. You can add and parse the data yourself to meet your needs. See MockContentResolver4.6 The context test Android provides two context test Classes A. isolatedcontext. This class allows you to test application data operations without affecting the real display of devices. It provides independent context, files, directories, and databases for use during testing. B. The renamingdelegatingcontext class quickly provides independent data operations for data operations and maintains the normal functions of other context operations. V. test executionTest runner is used to execute test cases. It can load test classes, set ups, runs, and destroy each test case. To perform the test, you must specify test runner in the manifest file. Instrumentationtestrunner supports all types of tests and can run all test cases.. You can specify it or its subclass in the instrumentation node of the manifest file. The instrumentationtestrunner code is included in the public class library Android. Test. runner, but not in the android code. To use it, you must declare it in the uses-library node. You do not need to manually create these projects. Creating projects through ide or command line is completed automatically. This solves the problem of always using instrumentationtestrunner in the above structure diagram. After the system loads and starts the test package, it will kill the original application process, restart the instance, and then give the control to the instrumentationtestrunner.. 6. view test resultsThe android test result is returned to the tool that starts it. You can view the name and method of the running test case and the specific error lines. Eclipse will be displayed in the new JUnit panel, and the command line will be displayed in the standard output. 7. Monkey and monkeyrunnerThe SDK provides two function-level application testing tools. A. UI/Application Exerciser monkey is often called Monkey. Use a command line tool run by ADB to send event streams that indicate keystrokes, touches, or gestures to the device, Perform stress tests on systems or specific applications,And reports errors automatically. Example: ADB shell monkey-p your. package. name-V 500 B. monkeyrunner provides an API and execution environment for compiling Python programs. You can control Android devices, such as installing and running applications and test packages, in addition to Android code, send the event stream to the program, and save the page. The monkeyrunner tool is different from the monkey (UI/Application Exerciser monkey) tool ). Monkey is a command line tool running on ADB. It is used to generate user and System Event streams and is random. monkeyrunner sends specific commands and events through APIS, generally, you need to write your own Python program to call the API.
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.