如何使用IntelliJ在模拟器中运行android测试?

时间:2012-01-20 03:20:06

标签: android unit-testing android-emulator intellij-idea

我正在尝试为android SDK附带的示例代码运行示例单元测试。有些测试作为标准单元测试运行,没问题。其他测试,如com.example.android.apis.view.Focus2AndroidTest,使用android类,因此需要在模拟器中运行,这是一个问题。

我可以在我的模拟器中运行应用程序。但是,当我运行测试时,我的模拟器启动,然后就坐在那里。测试永远不会运行。我在IntelliJ的输出窗口中看到的只是

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

我做错了什么?

编辑: 在@ CrazyCoder的建议之后,我切换到了android 4.0.3(API 15)。这是我得到的: 测试于下午4:34开始......

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.

我在logcat中看到的唯一相关内容是

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

3 个答案:

答案 0 :(得分:2)

您可以在模拟器或USB设备中运行普通应用吗?尝试创建新的模拟器设备,看看是否有帮助。

我在Windows上尝试使用IDEA 11.0.1,4.0.3 Android平台,它运行良好:

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

您还可以尝试使用adb kill-server重新启动adb,然后从AVD管理器手动启动模拟器。

验证模拟器中没有预先安装的ApiDemos应用程序,否则签名将不匹配。卸载现有的ApiDemos,然后尝试从IntelliJ IDEA部署并运行ApiDemos,然后尝试再次运行测试配置。


更新:我们做了一些研究,找到了问题的根源。默认情况下,IntelliJ IDEA将测试模块中应用程序模块的依赖范围设置为编译,以便将所有生产和测试类编译到单个Test.apk中。

相反,范围必须设置为提供,我们会在下次更新时修复它。现在您需要手动更正它,如屏幕截图所示:

Provided scope

重建项目并再次运行测试,这次将部署2个单独的apk文件,一个用于主应用程序,第二个用于测试,然后测试将运行:

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

答案 1 :(得分:0)

您可以从脚本的命令行运行ant中的检测。如果您更喜欢后者,post可能会有所帮助。

答案 2 :(得分:-1)

在模拟器中运行单元测试不实用(基本上需要太长时间)。而且由于android jar只对编译有用,因此使用单元测试运行它们似乎是不可能的。好的替代方案是使用先进的模拟框架(我个人更喜欢jMockit, 但还有其他的)

参见示例:

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

这里我测试我的类对派生的android活动,它必须调用超类方法 (这在maven,eclipse或IDEA当场运行)