android-maven-plugin, instrumentation testing and testSize



我正在使用maven来构建,运行和检测我的Android应用程序。Android测试框架有三种不同的测试范围@SmallTest,@MediumTest和@LargeTest,android-maven-plugin能够通过testTestSize或test/testSize参数选择测试范围。此参数可以是small|medium|large之一,可以从相关范围运行测试。

但是,如果我想同时运行小型和中型测试(不仅是小型测试还是仅中等测试),该怎么办?存在此问题的任何解决方案?

根据 InstrumentationTestRunner API 文档,这就是 Android SDK 的设计方式,目前应该工作:

运行所有小测试:adb shell am instrument -w -e size small com.android.foo/android.test.InstrumentationTestRunner

运行所有介质测试:adb shell am instrument -w -e size medium com.android.foo/android.test.InstrumentationTestRunner

运行所有大型测试:adb shell am instrument -w -e size large com.android.foo/android.test.InstrumentationTestRunner

即使使用普通 adb 命令运行测试,也必须使用两个进程分别运行中小型测试,一个接一个。Android Maven 插件只是 adb 命令的另一个包装器,因此无法通过 android-maven-plugin 配置 AFAIK 更改默认行为。

<小时 />

如果您更仔细地阅读 InstrumentationTestRunner API 文档,您会注意到有一个有趣的命令用法:

将测试运行过滤到具有给定注释的测试:adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.test.InstrumentationTestRunner

如果与其他选项一起使用,则生成的测试运行将包含两个选项的并集。 例如,"-e size large -e annotation com.android.foo.MyAnnotation" 将仅运行同时使用 LargeTest 和 "com.android.foo.MyAnnotation" 注释的测试。

注释配置作为实验性 API 添加(标记为 @hide,有关更多详细信息,请查看此版本历史记录),并且尚未记录在 am 仪器选项列表中。从理论上讲,您可以创建自己的注释类(参见 SmallTest.java 作为示例),标记所有@MediumTest以及您的@CustomizedTest,并使用 -e 大小和 -e 注释来实现您想要的:在一个命令中同时从两个注释运行联合测试。

不幸的是,android-maven-plugin 不支持注释配置,请参阅插件文档和最新的源代码。一种可能的解决方法是使用 exec-maven-plugin 运行纯adb shell am instrument命令。

希望这是有道理的。

为了使用 maven-android 的测试大小,我在 pom 中创建了一个变量.xml:

...
<properties>
  <config.build.testSize>medium</config.build.testSize>
</properties>
...

并在构建中,执行以下操作:

<build>  
 <pluginManagement>
  <plugins>
   <plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    ...
    <configuration>
     <test>
      <testSize>${config.build.testSize}</testSize>
     </test>
    </configuration>
    ...
   </plugin>
  </plugins>
 </pluginManagement> 
</build>

我假设,你也可以通过给 android.test.testSize 参数给 maven 来达到这个目的(比如 mvn install -Dandroid.test.testSize=medium)

如果我错了,请更正这个 maven 变量,还没有在文档中找到。溴,米.

最新更新