Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Gradle. How to build application and run tests from test application

I have android project and android test project inside it, located under folder tests. Structure of these project is eclipse like

+-src
| - res
| - libs
| - tests
|.....

I use gradle. All i want is to build app, run unit tests and get reports of them. But i don't understand how to do it right. I'm created build.gradle file in root folder

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"
    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 18
        testPackageName "ua.cooperok.stringcalc.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
        unitTest {
            java.srcDir file('tests/src')
            resources.srcDir file('tests/res')
        }
    }

    configurations {
        unitTestCompile.extendsFrom runtime
        unitTestRuntime.extendsFrom unitTestCompile
    }

    dependencies {
        unitTestCompile files("$project.buildDir/classes/release")
    }

    task unitTest(type:Test){
        description = "Run unit tests..."
        testClassesDir = android.sourceSets.unitTest.output.classesDir
        classpath = android.sourceSets.unitTest.runtimeClasspath
    }

    build.dependsOn unitTest

}

But when i run gradle build i got error
Could not find property 'output' on source set unit test.

Also, as i understand, to get reports i need to apply plugin "android-reporting", but when i do this i got error
Cannot add extension with name 'android', as there is an extension already registered with that name.

Should i do it like this, or i must create new gradle.build file in my test application project?

UPDATE

I fixed error with output, and made gradle build successful, but I don't understand what's goind on. I purposely failed my test, but gradle build is still successful.
What I've done:
1. I moved task unitTest from android section to root, after that, my test project began to compile, but there is an error 'packackage android.test not found'
2. To fix that error I added to classpath path to android sources, so after that gradle build was successful, but realy I don't understand why it's always successful even when test fails

And still I don't understand how to get reports from test

This is my new build.gradle file

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6+'
    }
}

apply plugin: 'android'

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"
    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 18
        testPackageName "ua.cooperok.stringcalc.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    sourceSets {
        main {
            manifest.srcFile file('AndroidManifest.xml')
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
    }
}

sourceSets {
    unitTest {
            java.srcDirs = ['tests/src']
            resources.srcDirs = ['tests/src']
    }
}

configurations {
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
}

dependencies {
    unitTestCompile files("$project.buildDir/classes/release")
    unitTestCompile 'junit:junit:4.11'
    compile fileTree(dir: 'libs', include: '*.jar')
}

task unitTest(type:Test, dependsOn: assemble) {
    description = "run unit tests"
    testClassesDir = sourceSets.unitTest.output.classesDir
    classpath = files("$System.env.ANDROID_HOME/sources/android-18")
    //actualy I didn't  get any report
    getReports().getJunitXml().setOutputPerTestCase(true)
}

build.dependsOn unitTest
like image 937
cooperok Avatar asked Nov 12 '13 11:11

cooperok


1 Answers

I found solution. I was dissapointed by few topics, and thought that i need to create separate task for unit tests. In fact it was prety easy.
I didn't change my project structure, just build.gradle file. To build application i run
gradle clean connectedCheck build
connectedCheck runs unit tests from my test project and if some test fails - gradle build will fails too

Here final version of it

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6+'
    }
}

apply plugin: 'android'

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"
    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 18
        testPackageName "ua.cooperok.stringcalc.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    sourceSets {
        main {
            manifest.srcFile file('AndroidManifest.xml')
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
        instrumentTest {
            java.srcDirs = ['tests/src']
            manifest.srcFile file('tests/AndroidManifest.xml')
            resources.srcDirs = ['tests/src']
            res.srcDirs = ['tests/res']
            assets.srcDirs = ['tests/assets']
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
    }

    //Signing apk
    if(project.hasProperty("signingPropertiesPath")) {
        File propsFile = new File(System.getenv('HOME') +  "/" + project.property("signingPropertiesPath"))
        if(propsFile.exists()) {
            Properties props = new Properties()
            props.load(new FileInputStream(propsFile))

            //loading keystore properties
            signingConfigs {
                release {
                    storeFile file(propsFile.getParent() + "/" + props['keystore'])
                    storePassword props['keystore.password']
                    keyAlias props['keyAlias']
                    keyPassword props['keyPassword']
                }
            }
            buildTypes {
                release {
                    signingConfig signingConfigs.release
                }
            }
        }
    }
}
like image 108
cooperok Avatar answered Nov 05 '22 16:11

cooperok