Eclipse Adt Test


まずはTest Projectを作ろう

It includes the same types of content and files, such as source code, resources, a manifest file, and so forth
The test package you create is connected to the application under test by an <instrumentation> element in its manifest file.

        android:targetPackage="com.example.パッケージ名" />



File > New > Project > Android > Android Test Project


Test Packageを作る

このテストパッケージをEclipseでRunしたら、JUnitのテスト結果がJUnit Viewに出てきます。(まだテストコードを書いてない場合はRun失敗になるけど)



New>JUnit Test Case

「New JUnit Test Case」ダイアログが出ます。
  1. 親クラスをAndroidTestCaseにすること。そうしないとRun Android JUnit Testしても失敗します。Run JUnit Testしか通りません。


  • Class under test:でテスト対象のクラスを選ぶ


    public void test関数名() {
        fail("Not yet implemented");



Run As>Android JUnit Test


Running tests from the command line

If you've created your tests in Eclipse,
you can still run your tests and test suites by using command-line tools included with the Android SDK.
You may want to do this, for example, if you have a large number of tests to run, if you have a large test case, or if you want a fine level of control over which tests are run at a particular time.

To run tests created in Eclipse with ADT with command-line tools,
you must first install additional files into the test project using the android tool's "create test-project" option.

When you run a test package in Eclipse with ADT,
the output appears in the Eclipse JUnit view.
You can run the entire test package or one test case class.
To do run tests, Eclipse runs the adb command for running a test package, and displays the output, so there is no difference between running tests inside Eclipse and running them from the command line.

As with any other package, to run a test package in Eclipse with ADT you must either attach a device to your computer or use the Android emulator. If you use the emulator, you must have an Android Virtual Device (AVD) that uses the same target as the test package.

To run a test in Eclipse, you have two choices:

Run a test just as you run an application, by selecting

Run As… > Android JUnit Test

from the project's context menu or from the main menu's Run item.
Create an Eclipse run configuration for your test project.
This is useful if you want multiple test suites, each consisting of selected tests from the project
. To run a test suite, you run the test configuration.
Creating and running test configurations is described in the next section.
To create and run a test suite using a run configuration:

In the Package Explorer,
select the test project, then from the main menu,

Run > Run Configurations….

The Run Configurations dialog appears.
In the left-hand pane,
find the Android JUnit Test entry.
In the right-hand pane,
click the Test* tab.
The Name: text box shows the name of your project.
The Test class: dropdown box shows one of the test classes in your project.
To run one test class,
click **Run a single test
then enter your project name in the Project: text box and the class name in the Test class: text box.
To run all the test classes, click Run all tests in the selected project or package, then enter the project or package name in the text box.
Now click the Target tab.
Optional: If you are using the emulator, click Automatic, then in the Android Virtual Device (AVD) selection table, select an existing AVD.
In the Emulator Launch Parameters pane, set the Android emulator flags you want to use. These are documented in the topic Android Emulator.
Click the Common tab.
In the Save As pane,
click Local to save this run configuration locally, or click Shared to save it to another project.
Optional: Add the configuration to the Run toolbar and the Favorites menu: in the Display in Favorites pane click the checkbox next to Run.
Optional: To add this configuration to the Debug menu and toolbar, click the checkbox next to Debug.
To save your settings, click Close.
Note: Although you can run the test immediately by clicking Run,
you should save the test first and then run it by selecting it from the Eclipse standard toolbar.
On the Eclipse standard toolbar, click the down arrow next to the green Run arrow.
This displays a menu of saved Run and Debug configurations.
Select the test run configuration you just created. The test starts.
The progress of your test appears in the Console view as a series of messages. Each message is preceded by a timestamp and the .apk filename to which it applies. For example, this message appears when you run a test to the emulator, and the emulator is not yet started:

Message Examples

The examples shown in this section come from the SpinnerTest sample test package,
which tests the Spinner sample application. This test package is also featured in the Activity Testing tutorial.

[yyyy-mm-dd hh:mm:ss - testfile] Waiting for HOME ('android.process.acore') to be launched…

In the following description of these messages, devicename is the name of the device or emulator you are using to run the test,
and port is the port number for the device.
The name and port number are in the format used by the adb devices command. Also,
testfile is the .apk filename of the test package you are running, and appfile is the filename of the application under test.

If you are using an emulator and you have not yet started it, then Eclipse first starts the emulator. When this is complete, you see the message:
HOME is up on device 'devicename-port'
If you have not already installed your test package, then you see the message:
Uploading testfile onto device 'devicename-port'
then the message Installing testfile.
and finally the message Success!
The following lines are an example of this message sequence:

[2010-07-01 12:44:40 - MyTest] HOME is up on device 'emulator-5554'
[2010-07-01 12:44:40 - MyTest] Uploading MyTest.apk onto device 'emulator-5554'
[2010-07-01 12:44:40 - MyTest] Installing MyTest.apk…
[2010-07-01 12:44:49 - MyTest] Success!

Next, if you have not yet installed the application under test to the device or emulator, you see the message
Project dependency found, installing: appfile
then the message Uploading appfile onto device 'devicename-port'
then the message Installing appfile
and finally the message Success!
The following lines are an example of this message sequence:

[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 see the message Launching instrumentation instrumentation_class on device devicename-port
instrumentation_class is the fully-qualified class name of the instrumentation test runner you have specified (usually InstrumentationTestRunner.
Next, as InstrumentationTestRunner builds a list of tests to run, you see the message
Collecting test information
followed by
Sending test information to Eclipse
Finally, you see the message Running tests, which indicates that your tests are running. At this point, you should start seeing the test results in the JUnit view. When the tests are finished, you see the console message Test run complete. This indicates that your tests are finished.
The following lines are an example of this message sequence:
[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 results appear in the JUnit view. This is divided into an upper summary pane, and a lower stack trace pane.

The upper pane contains test information. In the pane's header, you see the following information:

Total time elapsed for the test package (labeled Finished after x seconds).
Number of runs (Runs:) - the number of tests in the entire test class.
Number of errors (Errors:) - the number of program errors and exceptions encountered during the test run.
Number of failures (Failures:) - the number of test failures encountered during the test run. This is the number of assertion failures. A test can fail even if the program does not encounter an error.
A progress bar. The progress bar extends from left to right as the tests run. If all the tests succeed, the bar remains green. If a test fails, the bar turns from green to red.
The body of the upper pane contains the details of the test run. For each test case class that was run, you see a line with the class name. To look at the results for the individual test methods in that class, you click the left arrow to expand the line. You now see a line for each test method in the class, and to its right the time it took to run. If you double-click the method name, Eclipse opens the test class source in an editor view pane and moves the focus to the first line of the test method.

The results of a successful test are shown in figure 1.

Messages for a successful test
Figure 1. Messages for a successful test.

The lower pane is for stack traces. If you highlight a failed test in the upper pane, the lower pane contains a stack trace for the test. If a line corresponds to a point in your test code, you can double-click it to display the code in an editor view pane, with the line highlighted. For a successful test, the lower pane is empty.

The results of a failed test are shown in figure 2.

Figure 2. Messages for a test failure.

Except as noted, this content is licensed under Creative Commons Attribution 2.5. For details and restrictions, see the Content License.
About Android | Legal | Support

android eclipse junit test

サポートサイト androideclipsejunittest