Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从gradle运行单个Android(单元)测试,而不加载其他项目依赖项_Android_Unit Testing_Junit_Gradle_Robolectric - Fatal编程技术网

从gradle运行单个Android(单元)测试,而不加载其他项目依赖项

从gradle运行单个Android(单元)测试,而不加载其他项目依赖项,android,unit-testing,junit,gradle,robolectric,Android,Unit Testing,Junit,Gradle,Robolectric,我使用的是Android单元测试。我不厌其烦地运行这些单元测试的目的是为了提高速度(TDD快速反馈等等) 我已成功地对其进行了正确配置,并按如下方式运行了一些示例测试: ./gradlew test package com.mycompany.mysampleapp; import org.junit.Test; import static org.fest.assertions.api.Assertions.assertThat; public class AdditionOperat

我使用的是Android单元测试。我不厌其烦地运行这些单元测试的目的是为了提高速度(TDD快速反馈等等)

我已成功地对其进行了正确配置,并按如下方式运行了一些示例测试:

./gradlew test
package com.mycompany.mysampleapp;

import org.junit.Test;

import static org.fest.assertions.api.Assertions.assertThat;

public class AdditionOperationsTest {
  @Test public void testModulus() {
    assertThat(1).isEqualTo(1);
  }
}
无论何时运行测试,我都会注意到以下输出:

Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
The Test.testReportDir property has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the Test.getReports().getHtml().getDestination() property instead.
The TaskContainer.add() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the create() method instead.
:mySampleApp:preBuild UP-TO-DATE
:mySampleApp:preDebugBuild UP-TO-DATE
:mySampleApp:preReleaseBuild UP-TO-DATE
:libraries:facebook:compileLint
:libraries:facebook:copyReleaseLint UP-TO-DATE
:libraries:facebook:mergeReleaseProguardFiles UP-TO-DATE
:libraries:facebook:packageReleaseAidl UP-TO-DATE
:libraries:facebook:preBuild UP-TO-DATE
:libraries:facebook:preReleaseBuild UP-TO-DATE
:libraries:facebook:prepareReleaseDependencies
:libraries:facebook:compileReleaseAidl UP-TO-DATE
:libraries:facebook:compileReleaseRenderscript UP-TO-DATE
:libraries:facebook:generateReleaseBuildConfig UP-TO-DATE
:libraries:facebook:mergeReleaseAssets UP-TO-DATE
:libraries:facebook:mergeReleaseResources UP-TO-DATE
:libraries:facebook:processReleaseManifest UP-TO-DATE
:libraries:facebook:processReleaseResources UP-TO-DATE
:libraries:facebook:generateReleaseSources UP-TO-DATE
:libraries:facebook:compileRelease UP-TO-DATE
:libraries:facebook:processReleaseJavaRes UP-TO-DATE
:libraries:facebook:packageReleaseJar UP-TO-DATE
:libraries:facebook:packageReleaseLocalJar UP-TO-DATE
:libraries:facebook:packageReleaseRenderscript UP-TO-DATE
:libraries:facebook:packageReleaseResources UP-TO-DATE
:libraries:facebook:bundleRelease UP-TO-DATE
:mySampleApp:prepareComAndroidSupportAppcompatV71800Library UP-TO-DATE
:mySampleApp:preparemySampleAppandroidLibrariesFacebookUnspecifiedLibrary UP-TO-DATE
:mySampleApp:prepareDebugDependencies
:mySampleApp:compileDebugAidl UP-TO-DATE
:mySampleApp:compileDebugRenderscript UP-TO-DATE
:mySampleApp:generateDebugBuildConfig UP-TO-DATE
:mySampleApp:mergeDebugAssets UP-TO-DATE
:mySampleApp:mergeDebugResources UP-TO-DATE
:mySampleApp:processDebugManifest UP-TO-DATE
:mySampleApp:processDebugResources UP-TO-DATE
:mySampleApp:generateDebugSources UP-TO-DATE
Gradle似乎正在加载我的项目的所有依赖项

我的样本测试如下:

./gradlew test
package com.mycompany.mysampleapp;

import org.junit.Test;

import static org.fest.assertions.api.Assertions.assertThat;

public class AdditionOperationsTest {
  @Test public void testModulus() {
    assertThat(1).isEqualTo(1);
  }
}
这个测试实际上需要几分之一秒的时间来运行。我的理解是,所有项目依赖项的预加载都让它陷入困境

在好日子里,我会确保在类路径中有我需要的东西,然后运行类似于:

javac src/test/java/main/java/com/micromobs/pkk/AdditionOperationsTest.java
java org.junit.runner.JUnitCore com.micromobs.pkk.AdditionOperationsTest
考虑到这是一个带有gradle的Android项目,我假设我必须做一些不同的事情,比如在gradle构建文件中创建特定的任务,这些任务只包括示例项目的测试文件,然后运行gradle命令。/gradlew taskName

问题:是否可以单独在我的项目(com.mycompany.mysampleapp)的上下文中运行单个测试“AdditionOperationsTest”,这样它就不会加载外部项目依赖项 以下是我的配置文件当前的样子:

# settings.gradle
include ':libraries:gradle-android-test-plugin'
include ':libraries:facebook', ':mysampleapp'

# build.gradle
...
apply plugin: 'android-test'

dependencies {
    testCompile 'junit:junit:4.10'
    testCompile 'org.robolectric:robolectric:2.1.+'
    testCompile 'com.squareup:fest-android:1.0.+'
}

# location of my test files:
androidproj/mysampleapp/src/test/java/main/com/mycompany/mysampleapp/AdditionOperationsTest.java

您应该能够通过使用
-a
(不重建项目依赖项)来执行此操作。执行
gradle-测试
会导致
库:facebook
mysampleapp
项目无法重建


编辑:如下所述,您可以通过使用大大提高Gradle构建的性能。

我正在尝试解决同样的问题。我认为应该做的是有多个模块的项目,这样测试就存在于一个单独的模块中,但我希望这不是必要的。是的,我真的不想把我的测试分成不同的模块。我觉得把我们的整个项目转移到格拉德尔会让我失去好处。我希望这只是一些愚蠢的参数,我必须添加进去,或者我必须声明的任务,它将忽略外部项目依赖项,并运行我所指向的确切测试,并且只运行它所需要的依赖项。感谢Benjamin,哇,这是完全正确的。添加参数确保facebook库没有重建。不过我注意到,我只剃掉了几毫秒。没有显著地提高我的测试运行速度:(你能提供什么技巧来提高测试运行速度,例如,只有在使用时才延迟加载依赖项?我的测试没有使用“框架”的任何部分)所以应该跑得很快。干杯。我不知道。你的测试需要多长时间运行?你可能想看看为什么它实际上需要那么长的时间(例如,测试是否调用外部系统)。总时间:10.612秒。我认为这更多地与设置有关,因为每个测试类只是一个虚拟测试类(与我在问题中发布的测试类完全相同)应该立即运行。我认为gradle正在重新计算/重新编译,或者在运行单个测试之前做一些准备工作。一旦下载了依赖项,远程检查应该很快。顺便说一句:另一个技巧-你使用吗?那应该也会节省一些时间。你是我的朋友,是摇滚明星!我没有使用在你提到之前,我一直在使用守护进程。这将我的构建时间从10缩短到3秒。