Address: http://developer.android.com/tools/testing/testing_eclipse.html
This article describes how to create and run an Android Application Test in eclipse with the ADT plug-in. Before reading this article, you should know how to use the ADT plug-in to create and run Android applications through the basic steps.
And runningfrom eclipse is described in. You may need to read about building and runningfrom eclipse, which provides an overview of Android testing.
The ADT plug-in provides the following features to help you quickly build and manage the test environment:
- It can quickly create a test project and link it to the tested application. When it creates a test project, it automatically inserts the required
<instrumentation>
Element.
- It allows you to easily import the classes of the tested applications so that your test items can check them.
- You can use it to create runtime parameters for the test package and put them into the flag passed into the android testing framework.
- It allows you to run the test package without leaving eclipse. ADT automatically compiles the test package and test package, installs them on the device or simulator, and runs the test package, the test results are displayed in a separate window of Eclipse.
If you are not developing in eclipse or want to learn how to use the command line tool to create and run a test, see testing from other ides.
Create a test project
To set a test environment for your Android Application, you must first create a separate project to manage the test code. This new project follows the directory structure of a general Android application. It contains the same content and files, such as source code, resource files, a manifest file, and others. The newly created test package passes<instrumentation>
Element link to the tested application.
The "Create an android test project" dialog box allows you to easily create a test project with a clear directory structure. Its manifest file already contains<instrumentation>
Element. You can use the "Create an android test project" dialog box to create a test project at any time. This dialog box appears after you create an android application, but you can also run it to create a test project for a previously created Project.
Create a test project in Eclipse:
1. In eclipse, select File> New> other. A "select a wizard" dialog box appears.
2. in the "Wizard" list in the dialog box, find "android", click the switch button on the left, select "android Test Project", and click "Next" at the bottom of the dialog box ", the wizard "Create an android test project" appears.
3. enter the project name next to the test project name. You can use any name, but you should want to associate the name with the Project Name of the tested application, A good solution is to add the string "test" after the name of the project to be tested and use it as the name of the test project.
4. In the "content" section, check the recommended project path. If "use default location" is selected, the wizard will connect the workspace path with the project name you entered as the recommended path. For example, if your workspace path is/usr/local/workspace
Your project name isMyTestApp
, The Wizard will recommend/usr/local/workspace/MyTestApp
. Enter the desired path, deselect "use default path", and then enter or browse the desired project directory.
To learn more about the path selection of the test project, you can participate in testing fundamentals.
5. In the "test target" section, select "existing projects" and then select the Android Application in the list. You can see that the wizard has completed the test target, application name, and package name for you (the other two are in the property panel ).
6. On the "Compilation target" Panel, select the android SDK version used by the tested application.
7. Click "finish" to complete the wizard. If "finished" is not available, view the error information at the top of the wizard and fix it.
Create a test package
After creating a test project, you need to use the test package. Activity is not required for this package. You can also define it if you like. Although your test package integrates activity, test case, and other classes, your main test case should inherit from an android test case class or JUnit class, because they provide test features.
The test package does not require a GUI. When you run the test package in eclipse, the results are displayed in the JUnit view. The running test and view results are described in more detail in the running tests section.
A new test package starts with an android test case class in Android. Test. These classes inheritTestCase
In Android, the activity test class provides the instrumentation for the activity test. For more information about the test case class, see
Testing fundamentals.
Before creating a test package, you should select a package name and Android package name for your test case class. For more information, see testing fundamentals.
To add a test case class to the project:
1. on the "Project Browser" tab, open your test project and open the SRC folder.
2. Find the Java package name set by the new project wizard. If you have not added a class, there will be no content under the package name, and its icon is not filled. If you want to modify the package name, right-click the package name and select refactor> rename, and enter a new package name.
3. then, right-click the package name and select new> class. A "New Java class" dialog box is displayed, both "source folder" and "package" have been set.
4. In the "name" area, enter a name for the test case class. A method to set the class name is to add "test" to the component class you test ". For example, if you are testing the myappactivity class, your test case class name is myappactivitytest. The modifier of the test case class is set to public.
5. In the "parent class" column, enter the name of your inherited Android test case class. You can also browse available classes.
6.Which method stubs wowould you like to create?"Bar, cancel all options, and click" end ". You will set the constructor manually.
7. Your new class will appear in a new Java editing panel.
You should make sure that the constructor is correctly set and create a non-argument constructor for your class. JUnit needs it. Call the constructor of the parent class in the first statement of the constructor. Each test case parent class has its own constructor signature. For more information, seeandroid.test
For more information.
To control the test environment, you need to override the setup () and teardown () methods:
- Setup (): This method is called before each test method in the class is executed. It can be used to initialize the test environment. You can instantiate an intent whose action is action_main in setup (), and then start the activity using this intent in the test.
- Teardown (): This method is called after each test method in the class is executed. You can use it to perform garbage collection and reset the test environment.
Another useful method is to addtestPreconditions()
This method is used to check whether the tested package is correctly initialized. If the test item fails, the initial test environment is faulty. If so, the subsequent test results are doubtful, regardless of whether the test is successful or not.
The activity testing tutorial contains more information about creating test classes and methods.
Run the test
When you run a test package in eclipse with the ADT plug-in installed, the output will appear in the JUnit view of Eclipse. You can run the entire test package or give it a test case class. Eclipse runs a test package and displays the output by executing the ADB command. Therefore, there is no difference between running the test in eclipse and running the test from the command line.
Just like other application packages, to run a test package in eclipse, you must connect a device to your computer or use a simulator. If you use a simulator, you must have an android virtual device with the same API version as the test package.
There are two ways to perform a test in Eclipse:
- Run the test like running an application. Right-click the project or choose "Run as"> "android JUnit test" from the "run" menu ".
- Create a running parameter for the test project. This is useful when there are multiple sets of tests. Each set of tests contains the test items selected from the project. You can run the entire set of tests by executing the running parameters.
You must use the running parameters to create and run a complete set of tests:
1. In the package browser, select the test project and choose "run"> "Run configurations" from the main menu. The "Run Parameters" dialog box is displayed.
2. find "android JUnit test" in the yesterday's pane, and click "test" in the right pane. The text box "name:" displays the project name. The drop-down box "test class: "shows a test class in the project.
3. to run a separate test class, select "run a single test", enter the project name in the "Project:" text box, and enter the class name in the "test class" text box; to run all the test classes, select "Run all tests in the selected project, or package", and enter the project name or package name in the text box.
4. click "target". If you are using a simulator, select "automatic" and select an existing AVD In the android Virtual Device table, set the android simulator flag on the simulator startup parameters panel, which is described in detail in Android emulator.
5. click the "common" column. In the "Save as" section, select "local file" to save the running parameters for your own use only, or select "Shared File" to save them to other projects.
6. Add the running parameters to the "run" toolbar and "favorites" menu: check box next to "display in favorites menu" Panel "run.
7. Add the running parameters to the "debug" menu and toolbar, and select the check box next to "debug.
8. To save your settings, click "close. Note: although you can click "run" to run the test immediately, you should save the test parameters and select and run them from the eclipse toolbar.
9. In the eclipse toolbar, click the drop-down arrow next to the Green "run" arrow to display the list of saved running and debugging parameters.
10. Select the running parameters you created before and the test starts.
The project to be tested is displayed in the form of a message in the console, and each message comes with a time to match the .apk file name. For example, when you run a test through the simulator and the simulator is not started, the following message appears:
[yyyy-mm-dd hh:mm:ss - testfile] Waiting for HOME ('android.process.acore') to be launched...
In the following message description, "devicename" is the name of the device or simulator you are using for testing. "Port" is the port number of the device. Both "devicename" and "Port" are used.adb devices
Command Format. Similarly, "testfile" is the .apk File Name of the test package, and "appfile" is the file name of the application to be tested.
- If you are using a simulator and haven't started it, eclipse starts the simulator first, and then you will see the following message:
HOME is up on device 'devicename-port'
- If you have not installed the test package, you will see the following message:
Uploading testfile onto device 'devicename-port'
Then there is the message:
Installing testfile.
The last message is:
Success!
The following is an example of this message column:
[2010-07-01 12:44:49 - MyTest] Project dependency found, installing: MyApp[2010-07-01 12:44:49 - MyApp] Uploading MyApp.apk onto device 'emulator-5554'[2010-07-01 12:44:49 - MyApp] Installing MyApp.apk...[2010-07-01 12:44:54 - MyApp] Success!
- Next you will see the message:
Launching instrumentation instrumentation_class on device devicename-port
instrumentation_class
Is the full name of your specified Instrumentation Test runner, usuallyInstrumentationTestRunner
. NextInstrumentationTestRunner
To compile the test to be executed, you will see the message:
Collecting test information
Which is
Sending test information to Eclipse
Finally, you will see the message
Running tests
Indicates that the test is running. You should view the test result in the JUnit view. When the test ends, you will see the message
Test run complete
Indicates that the test has been completed. The following is an example of this message column:
[2010-01-01 12:45:02 - MyTest] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554[2010-01-01 12:45:02 - MyTest] Collecting test information[2010-01-01 12:45:02 - MyTest] Sending test information to Eclipse[2010-01-01 12:45:02 - MyTest] Running tests...[2010-01-01 12:45:22 - MyTest] Test run complete
The test result is displayed in the JUnit view, which is divided into the preceding description summary pane and the following trace stack information pane.
The preceding pane contains test information. The following information is displayed at the top of the pane:
- Total time of the test run ("XXX seconds later ").
- Number of test items (runs): the number of test items in the entire test class.
- Number of errors (errors): number of program errors and exceptions during the test.
- Number of failures: the number of times the test fails during the test. This is the number of assertion failures. The test item can fail without any program errors.
- A progress bar: The progress bar increases from left to right during the test. If all tests are successful, the progress bar remains green. If one test fails, the progress bar changes from green to red.
The form in the preceding pane contains the test run details. For each running test case class, you can see the rows with the class name. You need to view the test results of a single test method in the class, you can click the arrow on the left to expand this line. Now you can see each test method in the class and its running time. Double-click a test method. Eclipse opens the code for the test class in the editor pane and moves the focus to the first line of the test method.
The successful test result is shown in:
The following pane shows the trace stack information. If you select the failed test method in the preceding pane, the following pane displays the stack trace information for the test, if one line points to the place in your code, you can double-click it to display the code in the editor window, and the code line will be highlighted. If the test is successful, the following pane will be empty.
The following is the display when the test result is failed: