简体   繁体   中英

How do I run android tests in the emulator using IntelliJ?

I'm trying to run the sample unit tests for the sample code that ships with the android SDK. Some of the tests run as standard unit tests, no problem. Other tests, such as com.example.android.apis.view.Focus2AndroidTest, use android classes, and therefore need to run in the emulator, that is a problem.

I can run applications just fine in my emulator. When I run tests, though, my emulator starts up, and then just sits there. The test never runs. All I see in the output window in IntelliJ is

Waiting for device.
/Users/rfzabick/android-sdk-mac_x86/tools/emulator -avd MyAvd0 -netspeed full -netdelay none
Device connected: emulator-5554

What am I doing wrong?

EDIT: After @CrazyCoder's advice, I switched to android 4.0.3 (API 15). Here's what I got: Testing started at 4:34 PM ...

Waiting for device.
/Users/rfzabick/android-sdk-mac_x86/tools/emulator -avd android4.0.3--api15 -netspeed full -netdelay none -wipe-data -no-boot-anim
Device connected: emulator-5554
Device is online: emulator-5554
Target device: emulator-5554 (android4.0.3--api15)
Uploading file
    local path: /Users/rfzabick/IdeaProjects/ApiDemos/out/production/Tests/Tests.apk
    remote path: /data/local/tmp/com.example.android.apis.tests
Installing com.example.android.apis.tests
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
Device is not ready. Waiting for 20 sec.
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
Device is not ready. Waiting for 20 sec.
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
Device disconnected: emulator-5554
pkg: /data/local/tmp/com.example.android.apis.tests


Running tests
Test running startedTest running failed: com.android.ddmlib.AdbCommandRejectedException: device not found
Empty test suite.

The only relevant thing I see in logcat is

01-21 16:36:22.047: WARN/ActivityManager(91): No content provider found for permission revoke: file:///data/local/tmp/com.example.android.apis.tests

Can you run normal apps in the emulator or USB device? Try to create the new emulator device and see if helps.

I've tried it with IDEA 11.0.1, 4.0.3 Android platform on Windows and it works fine:

Waiting for device.
Target device: emulator-5554 (ICS)
Uploading file
    local path: D:\dev\android-sdk-windows\samples\android-15\ApiDemos\out\production\Tests\Tests.apk
    remote path: /data/local/tmp/com.example.android.apis.tests
Installing com.example.android.apis.tests
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
pkg: /data/local/tmp/com.example.android.apis.tests
Success


Running tests
Test running startedFinish

You can also try to restart adb with adb kill-server and start the emulator manually from AVD manager.

Verify that there is no pre-installed ApiDemos application in the emulator, or the signatures will not match. Uninstall existing ApiDemos, then try to deploy and run ApiDemos from IntelliJ IDEA, then try to run the test configuration again.


UPDATE: We did some research and found the source of the problem. By default IntelliJ IDEA sets the dependency scope for the application module inside test module to Compile , so that all the production and test classes get compiled into the single Test.apk.

Instead, the scope must be set to Provided and we'll fix it in the next update. Right now you need to correct it manually as shown on the screenshot:

提供范围

Rebuild the project and run the tests, again, this time 2 separate apk files will be deployed, one for the main app and the second one for the tests, then tests will run:

Waiting for device.
Target device: emulator-5554 (ICS)
Uploading file
    local path: D:\dev\android-sdk-windows\samples\android-15\ApiDemos\out\production\Tests\Tests.apk
    remote path: /data/local/tmp/com.example.android.apis.tests
Installing com.example.android.apis.tests
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests"
pkg: /data/local/tmp/com.example.android.apis.tests
Success


Uploading file
    local path: D:\dev\android-sdk-windows\samples\android-15\ApiDemos\out\production\ApiDemos\ApiDemos.apk
    remote path: /data/local/tmp/com.example.android.apis
Installing com.example.android.apis
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis"
pkg: /data/local/tmp/com.example.android.apis
Success


Running tests
Test running startedFinish

You can run the instrumentation from ant, from the command line of from a script. In case you prefer the latter this post may be of help.

Running unit tests in emulator is not practical (basically takes too long). And since android jars are usefull only to compiling, it seems impossiblble to run them with unit tests. Good alternative is use of advanced mocking framework ( I personally prefer jMockit, but there are other )

See example:

https://github.com/ko5tik/andject/blob/master/src/test/java/de/pribluda/android/andject/ViewInjectionTest.java

Here I test my class against derived android activity, and it has to call superclass methods (this runs in maven, eclipse or IDEA on the spot )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM