Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying test dependencies with the Gradle Android build system

I want to specify dependencies for my tests and after reading the Gradle Dependency Management Basics I though I could just add testCompile calls to my dependency declarations like this:

dependencies {     compile group: 'com.squareup.dagger', name: 'dagger', version: '1.0.0'       testCompile group: 'junit', name: 'junit', version: '4.11'     testCompile group: 'com.squareup', name: 'fest-android', version: '1.0.1' } 

This, however, fails with this error message:

> Could not find method testCompile() for arguments [{group=junit, name=junit, version=4.11}] on project ':simstatus'. 

Am I missing something here?

(Full gradle build file for reference)

like image 533
passy Avatar asked May 19 '13 17:05

passy


People also ask

How do you add Gradle dependency in build Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build. gradle file. This declares a dependency on an Android library module named "mylibrary" (this name must match the library name defined with an include: in your settings.

How do you define Gradle dependencies?

Gradle build script defines a process to build projects; each project contains some dependencies and some publications. Dependencies refer to the things that supports in building your project, such as required JAR file from other projects and external JARs like JDBC JAR or Eh-cache JAR in the class path.

How do I run a test on build Gradle?

In your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.


1 Answers

The android build system doesn't use the standard Gradle Java plugin.

Its documentation says:

As mentioned previously, next to the main sourceSet is the androidTest sourceSet, located by default in src/androidTest/

Additionally, the sourceSet can be configured to have its own dependencies. By default, the application and its own dependencies are added to the test app classpath, but this can be extended with

dependencies {     androidTestCompile 'com.google.guava:guava:11.0.2' } 

Update

As of May 2017 Doc, testCompile is deprecated and you should use testImplementation

dependencies {         androidTestImplementation 'com.google.guava:guava:11.0.2' } 
like image 192
JB Nizet Avatar answered Oct 05 '22 23:10

JB Nizet