有没有一种方法只能在Android Gradle项目中运行一组特定的工具测试? [英] Is there a way to only run a specific set of instrumentation tests in an Android Gradle project?

查看:93
本文介绍了有没有一种方法只能在Android Gradle项目中运行一组特定的工具测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android/Gradle项目.每当我想运行测试时,我都会运行:

./gradlew connectedInstrumentTest

它将在项目的测试文件夹下运行我的所有测试.

我的测试文件夹包含多个自动化测试以及非自动化测试.我最感兴趣的是运行快速的非自动化测试而没有慢速的自动化测试.

是否有一种方法可以只运行一组特定的测试,例如来自某个特定类的测试或类似的测试?我基本上是在问任何分离的问题,以便我可以选择只运行一些测试.


此处创建了示例项目.. >

编辑local.properties指向您的Android SDK.

接下来,启动仿真器或将电话连接到计算机.然后,您可以使用./gradlew connectedInstrumentTest --info运行测试.这将运行所有测试.

我无法弄清楚的是如何只在一个类而不是所有测试中运行测试.

解决方案

自Android Gradle插件1.3.0起

从1.3.0版开始,您可以(最终!)指定Android Gradle插件必须传递给InstrumentationTestRunner的参数.

例如,如果您只想运行带有@SmallTest注释的测试,而忽略其他测试,则:

android {
  //....
  defaultConfig {
  //....
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    testInstrumentationRunnerArgument "size", "small"
  }
}


旧的解决方法 在插件1.3.0之前是不可能的,但是我发现了一些解决方法.基本上,我已使用@SmallTest批注对快速测试进行了注释,并使用了InstrumentationTestRunner的自定义子类来运行它们,而不是整个套件.

您可以在此要点中找到示例代码.

I have an Android/Gradle project. Whenever I want to run tests, I run:

./gradlew connectedInstrumentTest

which runs all my tests under the test folder of my project.

My test folder has several automation tests as well as non-automation tests. I'm mostly interested in running the fast non-automation tests without the slow automation tests.

Is there a way to run just a specific set of tests, such as from one specific class or anything similar? I'm basically asking about any kind of separation so that I can choose to run just a few tests when I want to.


Created a sample project here.

Edit local.properties to point at your Android SDK.

Next, start up an emulator or connect a phone to your computer. Then you can run tests using ./gradlew connectedInstrumentTest --info. This runs all tests.

What I am unable to figure out is how to only run tests in, say, one class and not all tests.

解决方案

Since Android Gradle Plugin 1.3.0

Starting from version 1.3.0 you can (finally!) specify the arguments the Android Gradle Plugin have to pass to the InstrumentationTestRunner.

For example, if you want to run only the tests annotated with @SmallTest and ignore the others:

android {
  //....
  defaultConfig {
  //....
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    testInstrumentationRunnerArgument "size", "small"
  }
}


Old workaround Prior to plugin 1.3.0 is not possible to do that but I've found a little workaound. Basically I've annotated with the @SmallTest annotation the fast tests and using a custom subclass of the InstrumentationTestRunner I'm able to run just them and not the whole suite.

You can found the example code in this gist.

这篇关于有没有一种方法只能在Android Gradle项目中运行一组特定的工具测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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