gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:46:33 24 4
gpt4 key购买 nike

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

我已经设法正确配置它并运行了一些示例测试,如下所示:

./gradlew test

每当我运行测试时,我都会注意到以下输出:

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 似乎正在为我的项目加载所有依赖项。

我的示例测试如下:

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);
}
}

这个测试实际上应该只需要几分之一秒就可以运行。我的理解是,项目依赖项的所有预加载都会使其陷入困境。

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

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 command line option 来做到这一点(不重建项目依赖项)。执行 gradle -a test 应该会导致 libraries:facebookmysampleapp 项目不会被重建。

编辑:如下所述,您可以使用 Gradle daemon 显着提高 Gradle 构建的性能。 .

关于android - 从 gradle 运行单个 Android(单元)测试而不加载其他项目依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19286613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com