关于adb shell的使用

问题抛出:

怎样在cmd中执行命令让Android测试工程在Android模拟器或Android手机中自动运行进行android自动化测试呢?

解决方案:

使用adb shell命令

前置条件:

1.下载了android相关的工具android-sdk-windows并进行了环境变量的配置

2.将重新签名re-sign过的被测工程安装到Android模拟器或Android手机

3.将测试工程安装到Android模拟器或Android手机

解决过程:

1. 确保工程中的路径保存一致

AndroidManifest.xml文件中的test package名,注:此处是测试工程包,不是被测工程包:com.xqw.apk.test

Android测试工程下的Junit Test Class的路径需要与AndroidManifest.xml文件中保持一致:com.xqw.apk.test

2. 编写adb shell命令

执行某个TestCase: Junit Test Class

adb shell am instrument -w -e class com.xqw.apk.test.Test139Xqwcom.xqw.apk.test/com.neenbedankt.android.test.InstrumentationTestRunner

请注意:com.xqw.apk.test都是测试工程的包名,需要跟第1点中讲到的package name保持一致,否则可能会出现以下两个错误:

错误1:

java.lang.RuntimeException:Could not find testclass. class:com.xqw.apk.test.Test139Xqw

C:\Documents and Settings\Administrator>adb shell am instrument -w -e

class com.xqw.apk.test.Test139Xqw com.cplatform.xqw/android.test.InstrumentationTestRunne

r

INSTRUMENTATION_RESULT: shortMsg=java.lang.RuntimeException

INSTRUMENTATION_RESULT: longMsg=java.lang.RuntimeException: Could not

find testclass. Class: com.xqw.apk.test.Test139Xqw

INSTRUMENTATION_CODE: 0

解释:该错误的原因是找不到com.xqw.apk.test.Test139Xqw这个类,之前由于前面的包名不一致,AndroidManifest.xml文件中是com.xqw.apk.test,而工程中是com.apk.xqw.test,所以老是报该错误。

错误2:

INSTRUMENTATION_STATUS:Error=Unable to find instrumentation info for: Component

C:\Documents and Settings\Administrator>adb shell am instrument -w -e

class com

.xqw.apk.test.Test139Xqw

com.xqw.apk.test/android.test.InstrumentationTestRunner

INSTRUMENTATION_STATUS: id=ActivityManagerService

INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for:

Component

Info{com.xqw.apk.test/android.test.InstrumentationTestRunner}

INSTRUMENTATION_STATUS_CODE: -1

android.util.AndroidException: INSTRUMENTATION_FAILED:

com.xqw.apk.test/android.

test.InstrumentationTestRunner

解释:该错误的原因可能出在com.xqw.apk.test/com.neenbedankt.android.test.InstrumentationTestRunner 这边:

1) 可能是testpackage测试包名不对com.xqw.apk.test,那么按照上面说的调整就可以了

2)也可能是com.neenbedankt.android.test.InstrumentationTestRunner这个InstrumentationTestRunner调用的不对。默认情况下,android模拟器中的InstrumentationTestRunner是:android.test.InstrumentationTestRunner。

该com.neenbedankt.android.test.InstrumentationTestRunner是由于引用了testutils.jar结合输出junitreporter报告用的,如果不需要输入报告,则直接使用android.test.InstrumentationTestRunner即可。

执行所有的TestCase

adbshell am instrument –w com.xqw.apk.test/com.neenbedankt.android.test.InstrumentationTestRunner

这边可能会报如下错误

C:\Documents and Settings\Administrator>adb shell am instrument -w

com.xqw.apk.t

est/com.neenbedankt.android.test.InstrumentationTestRunner

com.cplatform.xqw.test.HttpToolsTest:..

Error in testRequestByHttpGet:

java.lang.IllegalStateException: Target host must not be null, or set in

paramet

ers.

at

org.apache.http.impl.client.DefaultRequestDirector.determineRoute(Def

aultRequestDirector.java:561)

at org.apache.http.impl.client.DefaultRequestDirector.execute

(DefaultReq

uestDirector.java:292)

at org.apache.http.impl.client.AbstractHttpClient.execute

(AbstractHttpCl

ient.java:555)

at org.apache.http.impl.client.AbstractHttpClient.execute

(AbstractHttpCl

ient.java:487)

at org.apache.http.impl.client.AbstractHttpClient.execute

(AbstractHttpCl

ient.java:465)

at com.cplatform.xqw.test.HttpToolsTest.testRequestByHttpGet

(HttpToolsTe

st.java:99)

at java.lang.reflect.Method.invokeNative(Native Method)

at android.test.AndroidTestRunner.runTest

(AndroidTestRunner.java:169)

at android.test.AndroidTestRunner.runTest

(AndroidTestRunner.java:154)

at android.test.InstrumentationTestRunner.onStart

(InstrumentationTestRun

ner.java:520)

at

com.neenbedankt.android.test.InstrumentationTestRunner.onStart(Instru

mentationTestRunner.java:45)

at android.app.Instrumentation$InstrumentationThread.run

(Instrumentation

.java:1447)

Error in testRequestByHttpPost:

java.lang.IllegalStateException: Target host must not be null, or set in

paramet

ers.

3. 将命令存成bat文件,就可以在windows下执行了

@echo on

adb shell am instrument -w -e class com.xqw.apk.test.Test139Xqw com.xqw.apk.test/com.neenbedankt.android.test.InstrumentationTestRunner

echo "测试执行完成....."

adb pull /data/data/com.cplatform.xqw/files/TEST-all.xml D:/TestResult

echo "将结果输出到PC本地磁盘....."

adb pull/data/data/com.cplatform.xqw/files/TEST-all.xml D:/TestResult 是将junitreporter测试报告结果从Android模拟器或Android手机中pull到本地PC磁盘D:/TestResult

4. 参考资料:

在Android_Robotium自动化测试中导出JunitTest Result Reporter:http://code.google.com/p/nbandroid-utils/

RunTestsCommand:

http://developer.android.com/tools/testing/testing_otheride.html#RunTestsCommand

InstrumentationTestRunner:

http://developer.android.com/reference/android/test/InstrumentationTestRunner.html


更多相关文章

  1. 关于 Android(安卓)下的自动化测试
  2. Android测试教程(16):monkeyrunner简介
  3. Android编程之manifest上遇到的错误
  4. Android常用异常及解决方案
  5. Android(安卓)调试:java 跨工程调试 android 项目
  6. Eclipse android 项目转android studio填坑之旅
  7. Android(安卓)调试:java 跨工程调试 android 项目
  8. Android使用XML文件定义用户界面
  9. 基于Eclipse的Android(安卓)JNI层测试应用开发过程记录

随机推荐

  1. 3D相机缩放并遵循java中的物理特性?
  2. 为什么HttpUrlConnection在移动数据连接
  3. Thrift项目Server端开发流程
  4. Akka ZeroMQExtension使用一个zeromq套接
  5. 关键词final的用途是什么?
  6. .Net JAVA JS 加密(三方互通)
  7. 增强的for循环中局部变量的范围
  8. javaScript函数中执行C#代码中的函数
  9. thinking in java逍遥游记 之 夜的第四章
  10. Maven编译提示:软件包不存在