如何使用 Gradle 运行一组特定的 Android 仪器测试? [英] How can I run a specific set of Android instrumentation tests with Gradle?

查看:25
本文介绍了如何使用 Gradle 运行一组特定的 Android 仪器测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个测试包:

com.mypackage.blackbox - Robotium UI 测试com.mypackage.integration - REST 集成测试com.mypackage.unit - 低级单元测试

我们的服务器团队需要能够在每次推送时只运行集成测试(需要几分钟),然后每晚运行所有测试(黑盒 UI 测试需要超过 10 分钟).

这个很好的答案通过重载像 <代码>@SmallTest 或 @LargeTest.

Gradle 文档 建议测试过滤器是这样做的方法,例如

./gradlew connectedAndroidTestDevDebug --tests com.mypackage.integration.*

然而,这会因 > 而失败;未知的命令行选项 '--tests'. 错误(大概是因为 Android Gradle 插件不支持 vanilla Gradle 所做的一切?).

同样的文档说他们计划在未来支持这些替代方案:

<块引用>
  • 基于自定义注释的过滤(未来)
  • 基于测试层次的过滤;执行扩展ceratain基类的所有测试(未来)
  • 基于一些自定义运行时规则进行过滤,例如系统属性或某些静态(未来)的特定值

有没有人知道一种干净的方法来让它立即工作?现在,我计划在所有集成测试扩展的基类上使用 @MediumTest 注释,但我希望能够指定特定的包.使用 @MediumTest@LargeTest 会滥用这些注释,因为我的集成测试和黑盒测试都是大型测试 根据指南.

解决方案

现在可以通过添加 Android 最新的 测试支持库,您现在可以使用 AndroidJUnitRunner 并通过您自己的自定义注释过滤您运行的测试.

过滤测试运行以使用自定义注释(在本例中为 com.myapp.MyAnnotation):

adb shell am \仪器 -w -e 注释 com.myapp.MyAnnotation \com.myapp/android.support.test.runner.AndroidJUnitRunner

完整的 AndroidJUnitRunner 文档

您需要使用自定义注释对测试用例进行注释才能使其正常工作.示例测试用例:

import android.support.test.runner.AndroidJUnit4;导入 com.myapp.MyAnnotation;@RunWith(AndroidJUnit4.class)公共类 CalculatorTest {@MyAnnotation@测试公共无效 testAddition() {//在这里做测试}}

您的MyAnnotation"如下所示:

package com.myapp;导入 java.lang.annotation.ElementType;导入 java.lang.annotation.Retention;导入 java.lang.annotation.RetentionPolicy;导入 java.lang.annotation.Target;/*** 我的自定义注释来指定要运行的测试类型.*/@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD, ElementType.TYPE})公共@interface MyAnnotation {}

I have multiple test packages:

com.mypackage.blackbox    - Robotium UI tests
com.mypackage.integration - REST integration tests
com.mypackage.unit        - low level unit tests

Our server team needs to be able to run just the integration tests on every push (they take a couple of minutes), but then run all tests every night (the black box UI tests take more than 10 minutes).

This great answer provides a slightly hacky (but effective) way to do it by overloading an existing JUnit annotation like @SmallTest or @LargeTest.

The Gradle documentation suggests that test filters are the way to do this, e.g.

./gradlew connectedAndroidTestDevDebug --tests com.mypackage.integration.*

However, that fails with an > Unknown command-line option '--tests'. error (presumably because the Android Gradle plugin doesn't support everything that vanilla Gradle does?).

The same documentation says in future they plan to support these alternatives:

  • Filtering based on custom annotations (future)
  • Filtering based on test hierarchy; executing all tests that extend ceratain base class (future)
  • Filtering based on some custom runtime rule, e.g. particular value of a system property or some static state (future)

Does anybody know a clean way to get this to work right now? For now I'm planning to use the @MediumTest annotation on the base class that all my integration tests extend, but I'd love to be able to specify particular package(s) instead. Using @MediumTest or @LargeTest abuses those annotations, as both my integration and black box tests are large tests according to the guidelines.

解决方案

This is now possible with the addition of Android's recent Testing Support Library, you can now use AndroidJUnitRunner and filter the tests you run by your own custom annotations.

Filter test run to tests with a custom annotation (com.myapp.MyAnnotation in this example):

adb shell am \
  instrument -w -e annotation com.myapp.MyAnnotation \
  com.myapp/android.support.test.runner.AndroidJUnitRunner

Complete AndroidJUnitRunner Documentation

You'll need to annotate your test cases with your custom annotation to get this to work. Example test case:

import android.support.test.runner.AndroidJUnit4;
import com.myapp.MyAnnotation;

@RunWith(AndroidJUnit4.class)
public class CalculatorTest {

    @MyAnnotation
    @Test
    public void testAddition() {
        //Do testing here
    }

}

Here is what your "MyAnnotation" would look like:

package com.myapp;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * My custom Annotation to specify a type of tests to run.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyAnnotation {
}

这篇关于如何使用 Gradle 运行一组特定的 Android 仪器测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆