有没有办法只在 Android Gradle 项目中运行一组特定的仪器测试?

时间:2023-02-01
本文介绍了有没有办法只在 Android Gradle 项目中运行一组特定的仪器测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

./gradlew connectedInstrumentTest

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

我的测试文件夹有几个自动化测试以及非自动化测试.我最感兴趣的是在没有慢速自动化测试的情况下运行快速的非自动化测试.

有没有办法只运行一组特定的测试,例如来自一个特定的类或类似的东西?我基本上是在询问任何类型的分离,以便我可以在需要时选择只运行几个测试.

<小时>

这里创建了一个示例项目..p>

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

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

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

解决方案

Android Gradle Plugin 1.3.0 起

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

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

<代码>android {//....默认配置 {//....testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"testInstrumentationRunnerArgument "size", "small"}}

<小时>

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

您可以在this gist中找到示例代码.

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 项目中运行一组特定的仪器测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:无法为 org.gradle.api.internal.artifacts.dsl.dependencies.Defau 下一篇:错误无法解析符号 TabLayout 和“设计"

相关文章

最新文章