Android basics 12: Android automated testing 03-JUnit-based Android testing framework 03

Android basics 12: Android automated testing 03-JUnit-based Android testing framework 03

Source: Internet
Author: User

This article describes how to run the JUnit test.

7. How to run the JUnit Test

We have learned a lot of JUnit programs and provided many features in the android ADT plug-in to facilitate unit testing. However, how to perform unit tests, for example, how to run the test example program provided in apidemos \ test, is a bit confusing for our beginners. I also searched for a lot of materials on the Internet, to know how to run the test unit. Therefore, I would like to share with you the summary here. To sum up, there are four different methods:

7.1 run the test unit using ADT
In es, right-click the project and choose Android JUnit test from the run as/debug as sub-menu options, as shown below:

After you click Run, the application starts and a new panel JUnit appears in ES, as shown below:

The test results are displayed on this interface. This method is relatively simple to operate, but to write unit tests on your own, you must have a deep understanding of the Methods running in the next step 2.

7.2 run the test unit through AVD

Run AVD and select Dev tool. The current interface is as follows:

After you double-click instrumentation, the interface is as follows:

Run the test example. Check the output information of the running process in logcat. The interface is as follows:

This information is the output of the test example.
7.3 run the test unit using the ADB shell command
This method should be set for the Linux programmer. It is relatively difficult to use the full command line. The command format is as follows:

According to the command line format above, enter: ADB shell am instrument-W com. xmobileapp. Hello/Android. Test. instrumentationtestrunner

The running interface is as follows:

The information of the test result is exactly the same as that of logcat in the 2nd methods.

7.4 Error Message description
When running the test example, you may encounter many error prompts, which are summarized as follows:
After you click "android JUnit test" to run it, the error message "android launch" appears, as shown below:

The cause of this error is: androidmanifest. xml configuration error. For more information, see
The description in the length of the instrumention test runner is not specified.
Run the "error = unable to find instrumentation info for..." command using the ADB shell am command ........" The error message is as follows:

Error = unable to find instrumentation info ........ Try
ADB shell PM list instrumentation check whether your device has installed this Instrumentation Test package. To use ADB shell am instrument-W, you must install the test package on the device.

7.5 Summary

After reading this, I am not very confused about the instrument. In the next article, we will learn about the instrument in the android SDK. To help you learn how to run the test unit, a simple example is provided for you to download. This example contains an activity (Hello) and a hellotest activity (hellotest) you can use the method described above to start the unit test.

8. Android JUnit learning Summary

As you learn more about Android and JUnit, sort out the classes of Android and JUnit as follows:
Test-testcase-androidtestcase
Test-testcase-instrumentationtestcase
Test-testsuite-instrumentationtestsuite
Testlistener -- basetestrunner-androidtestrunner
Instrumentation-

Instrumentationtestrunner
The above five routes are also a process of continuous learning. We feel that we have clearly resolved the first four routes, and the last one does not seem to be clear, later, I checked a lot of such information and explained the instrumentation again.
Each Android Application runs in its own process. instrumentation kills the current application and restarts the application (restarts the process with instrumentation ). Instrumentation provides us with a handle in the application context. With this handle, we can gain insight into the application to verify the test assertions, we can also use it to write test cases that are more underlying than interface tests. It should be emphasized that instrumentation cannot capture the bugs in the UI.
Android has extended three Instrumentation-related classes based on JUnit:

The above description comes from the web page Instrumentation
Testing (in English) is recommended here.

JUnit's usage experience

JUnit adopts the test-driven development method. That is to say, you should write the test code before development. It is mainly used to describe how the tested code is used and handle errors. Then you can write the code, and gradually test the code in the test code until it passes completely in the test code.
I checked whether I felt that some of them were not in line with the programmer's thinking habits (first writing code and then debugging). It is true that JUnit is a "Subversion" of the programmer's thinking habits ". Here I also feel that it is difficult to do so. Why? Before a "horse" is completely designed, how should we determine how the horse will run in the future? And even if we define how the "horse" will "run" in the future, what should we do if the "horse" is changed during implementation? After all, a person cannot have two roles at the same time, otherwise he may not know which role he is currently playing!
Speaking of this, I will explain my own use of JUnit "error". This may be in conflict with the purpose of JUnit test-driven development, but it can effectively reduce bugs. At its core, JUnit completely separates the source code from the test code and uses the test code as a separate program. The methods described earlier integrate the source code with the test code. Because the importance of the source code is greater than that of the test code, the test code is often incomplete and the structure is unclear, in this way, unit testing for programmers is incomplete. JUnit is used for complete unit tests to test the running results of some of the current Code in each "environment.

Postscript

This is a series of articles published on the Internet. I have tried my best to introduce JUnit. After reading these articles, we can have a preliminary understanding of the android JUnit test. I read these articles myself, and then I read the examples and some source code. I am not very familiar with some details. I will go into the relevant source code later, especially the applicationtestcase and runner sections.

References:

Android and JUnit

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.