【问题标题】:How do you run Android instrumentation tests from Eclipse?如何从 Eclipse 运行 Android 仪器测试?
【发布时间】:2010-10-02 20:45:30
【问题描述】:

目前我正在以这种方式从命令行运行仪器测试:

adb shell am instrument -w com.blah.blah/android.test.InstrumentationTestRunner

有没有办法从 Eclipse 运行它们(自动安装应用程序)?

【问题讨论】:

    标签: android eclipse testing


    【解决方案1】:

    我无法确定自动部署到模拟器。但是,您可以使用相同的“adb shell”命令并创建外部启动配置。我在同一主题上写了博客here。当您还使用“-e debug true”参数时,以这种方式启动会更直观。

    但是,我认为我已经从 bash shell 脚本中获得了更多的好处(如果您使用的是好的开发平台):

    function adbtest() {
        adb shell  am instrument -w -e class blah.package.$1 blah.package.test/android.test.InstrumentationTestRunner;
    }
    

    这样当我想测试 blah.package.FooTest 我只需要记住输入:

    james@trex:~$ adbtest FooTest
    

    【讨论】:

      【解决方案2】:

      我不知道从 Eclipse 自动运行测试的好方法,但我已经整理了一种使用 ant 自动构建和部署测试的直接方法。

      我的项目组织如下:

      1. 我们把项目根目录称为root
      2. 在里面,我有SDK中activityCreator脚本生成的build.xml。
      3. 我有第二个项目,其中包含位于 root/tests 中的测试
        • 本项目有自己的AndroidManifest.xml(以Android API Demos的结构为例)
        • 这个项目也有它自己的 build.xml

      为了在root/tests/build.xml中支持junit,需要添加junit的路径。一种方法是添加 compile、dex、debug 和 release 目标的路径(release 被省略,但它需要与 debug 相同的更改)。同样在编译目标中,我们包含 ../src 路径:

      <!-- Compile this project's .java files into .class files. -->
      <target name="compile" depends="dirs, resource-src, aidl">
          <javac encoding="ascii" target="1.5" debug="true" extdirs=""
                  srcdir="src/:../src"
                  destdir="${outdir-classes}"
                  bootclasspath="${android-jar}">
              <classpath>
                  <fileset dir="${external-libs}" includes="*.jar"/>
                  <fileset file="${junit-path}"/>
              </classpath>
           </javac>
      </target>
      
      <!-- Convert this project's .class files into .dex files. -->
      <target name="dex" depends="compile">
          <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
          <apply executable="${dx}" failonerror="true" parallel="true">
              <arg value="--dex" />
              <arg value="--output=${intermediate-dex-ospath}" />
              <arg path="${outdir-classes-ospath}" />
              <fileset dir="${external-libs}" includes="*.jar"/>
              <fileset file="${junit-path}"/>
          </apply>
      </target>
      
      <!-- Package the application and sign it with a debug key.
           This is the default target when building. It is used for debug. -->
      <target name="debug" depends="dex, package-res">
          <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>
          <exec executable="${apk-builder}" failonerror="true">
              <arg value="${out-debug-package-ospath}" />
              <arg value="-z" />
              <arg value="${resources-package-ospath}" />
              <arg value="-f" />
              <arg value="${intermediate-dex-ospath}" />
              <arg value="-rf" />
              <arg value="${srcdir-ospath}" />
              <arg value="-rj" />
              <arg value="${external-libs-ospath}" />
              <arg value="-rj" />
              <arg value="${junit-path}" />
              <arg value="-nf" />
              <arg value="${native-libs-ospath}" />
          </exec>
      </target>
      

      现在,我们可以分别构建这两个项目。最后一步是向 root/build.xml 添加一个新目标,它将构建和部署项目和测试并执行测试。 为此,将以下目标添加到 root/build.xml:

      <target name="tests" depends="reinstall">
          <echo>Building and installing tests..</echo>
          <exec executable="ant" failonerror="true">
              <arg value="-f" />
              <arg value="tests/build.xml" />
              <arg value="reinstall"/>
          </exec>
          <mkdir dir="${log-dir}" />
          <exec executable="${adb}">
          <arg value="shell" />
          <arg value="am" />
          <arg value="instrument" />
          <arg value="-w" />
          <arg value="-e" />
          <arg value="class" />
          <arg value="org.yourproject.AllTests" />
          <arg value="org.yourproject.tests/android.test.InstrumentationTestRunner" />
          </exec>
      </target>
      

      一切就绪后,启动模拟器并运行“ant 测试”。这将在一个命令中构建、部署和执行您的测试。

      【讨论】:

      • 这正是我所需要的,但我正试图弄清楚如何将它与机器人集成......你有一个例子吗?我是 ant 的新手,但我需要这样做,以便与 hudson 集成并完成整个自动化测试循环。
      猜你喜欢
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多