2

I'm not sure how to get my unit tests to run through Gradle. I created a unitTest task, set appropriate source sets, and appropriate dependencies, but I'm getting the error:

package android.test does not exist 

I followed directions in another post and added the following to my build.gradle:

// Unit tests sourceSets { unitTest { java.srcDir file('test') resources.srcDir file('test/res') } } dependencies { unitTestCompile files("$project.buildDir/classes/debug") unitTestCompile 'junit:junit:4.11' unitTestCompile 'com.google.android:android:4.0.1.2' } configurations { unitTestCompile.extendsFrom runtime unitTestRuntime.extendsFrom unitTestCompile } task unitTest(type:Test, dependsOn: assemble) { description = "run unit tests" testClassesDir = project.sourceSets.unitTest.output.classesDir classpath = project.sourceSets.unitTest.runtimeClasspath } 

Full build.gradle below:

buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.4.2' } } configurations { apt } apply plugin: 'android' dependencies { compile project(':PullToRefresh') compile project(':simple-crop-image-lib') compile project(':facebook') compile project(':gridlayout') compile project(':nineoldandroids') compile project(':ViewPagerIndicator') compile project(':volley') compile fileTree(dir: 'libs', include: '*.jar') apt files('compile-libs/androidannotations-2.7.1.jar') } android { buildToolsVersion '17.0' compileSdkVersion 17 signingConfigs { debug { storeFile file('keystores/debug.keystore') } } buildTypes { debug {} ipconfigurable { debuggable true signingConfig signingConfigs.debug } } sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } } } // Unit tests sourceSets { unitTest { java.srcDir file('src/test') resources.srcDir file('src/test/res') } } dependencies { unitTestCompile files("$project.buildDir/classes/debug") unitTestCompile 'junit:junit:4.11' unitTestCompile 'com.google.android:android:4.0.1.2' } configurations { unitTestCompile.extendsFrom runtime unitTestRuntime.extendsFrom unitTestCompile } task unitTest(type:Test, dependsOn: assemble) { description = "run unit tests" testClassesDir = project.sourceSets.unitTest.output.classesDir classpath = project.sourceSets.unitTest.runtimeClasspath } 

Directory Structure:

enter image description here

3
  • What is your directory structure for your tests? Specifically, do your directory names match up to your package names? Commented Feb 19, 2014 at 7:36
  • @PerrynFowler Thanks for your comments. I made sure that my directory names match up with the package names and updated the srcDir in sourceSets, but still getting same error. Directory structure included as image. Commented Feb 19, 2014 at 18:16
  • 1
    it looks to me like you need to add android-test to your unitTestCompileDependencies Commented Feb 20, 2014 at 9:30

1 Answer 1

4

Thanks to @PerrynFowler, the solution was to add the following line to my unitTestCompile dependencies:

unitTestCompile 'com.google.android:android-test:4.1.1.4' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.