简体   繁体   English

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

[英]Run a single Android (unit) test from gradle without loading other project dependencies

I'm using the awesome plugin from Jake Wharton for Android unit tests. 我正在使用Jake Wharton精彩插件进行Android单元测试。 My objective of taking the trouble to get these unit tests running, is for speed (TDD quick feedback and all). 我努力让这些单元测试运行的目的是为了速度(TDD快速反馈和所有)。

I've manage to configure it correctly and have some sample tests running as follows: 我已经设法正确配置它并运行一些示例测试,如下所示:

./gradlew test

Whenever I run the tests though I notice the following output: 每当我运行测试时,我会注意到以下输出:

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 seems to be loading ALL the dependencies for my project. Gradle似乎正在为我的项目加载所有依赖项。

My Sample Test is as follows: 我的样本测试如下:

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

This test should actually take a fraction of a second to run. 这个测试实际上应该只需要几分之一秒才能运行。 My understanding is that all that pre-loading of the project dependencies is bogging it down. 我的理解是,所有预先加载项目依赖项的行为都会让它陷入困境。

In the good days, I would make sure i have what i need in the CLASSPATH and just run something like: 在美好的日子里,我会确保我在CLASSPATH中拥有我需要的东西并运行如下:

javac src/test/java/main/java/com/micromobs/pkk/AdditionOperationsTest.java
java org.junit.runner.JUnitCore com.micromobs.pkk.AdditionOperationsTest

Considering this is an Android project with gradle, I'm assuming i would have to do something a little different like create specific tasks in the gradle build file, that include only my test files for my sample project, and then run a gradle command ./gradlew taskName ? 考虑到这是一个带有gradle的Android项目,我假设我必须做一些不同的事情,比如在gradle构建文件中创建特定任务,其中只包括我的示例项目的测试文件,然后运行gradle命令。 / gradlew taskName?

Question: Is it possible to run the single test "AdditionOperationsTest" within the context of my project (com.mycompany.mysampleapp) alone so that it doesn't load the external project dependencies 问题:是否可以单独在我的项目(com.mycompany.mysampleapp)的上下文中运行单个测试“AdditionOperationsTest”,以便它不会加载外部项目依赖项

Here's how my configuration files currently look like: 这是我的配置文件目前的样子:

# 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

You should be able to do so by using the -a command line option (no rebuild of project dependencies). 您应该可以使用-a 命令行选项 (不重建项目依赖项)来执行此操作。 Executing gradle -a test should result in the libraries:facebook and mysampleapp projects not being rebuilt. 执行gradle -a test应该导致libraries:facebookmysampleapp项目没有被重建。

EDIT: As noted below you can significantly improve the performance of your Gradle build by using the Gradle daemon . 编辑:如下所述,您可以使用Gradle守护程序显着提高Gradle构建的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gradle android,如何排除单个单元测试 - Gradle android, how to exclude a single unit test 如何使用 gradle 2.4 运行单个 Android 本地单元测试。 不支持测试过滤 - How to run single Android local unit test using gradle 2.4. Test filtering is not supported 编写Gradle脚本以运行Eclipse Android Test项目的单元测试用例 - Writing Gradle script to run unit test cases for Eclipse Android Test project 如何在针对特定 Gradle 构建风格的同时从命令行运行单个单元测试方法 - How to run a single unit test method from command line while targeting a specific Gradle build flavor 在TeamCity中运行android studio项目的单元测试 - Run unit test of android studio project in TeamCity 如何运行android单元测试项目 - how to run android unit test project 使用android gradle插件2.3。+的多项目测试依赖项 - Multi-project test dependencies with android gradle plugin 2.3.+ 运行单元测试时将 Android Gradle 插件从 3.5.3 更新到 3.6.1 后出现 OutOfMemoryError - Got OutOfMemoryError after update Android Gradle Plugin to 3.6.1 from 3.5.3 when run unit test 使用Gradle在Android中使用JUnit进行单元测试 - Unit test with JUnit in Android with Gradle Android Gradle未运行单元测试 - android gradle not running unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM