3
votes

I'm trying to test an app from the command line. When I run my test from the Android Studio, everything goes fine. But when I try to run the command:

./gradlew test

I get this error:

Task 'test' not found in root project 'mvp_kotlin_example'.

I don't have problems using ./gradlew build or ./gradlew clean. I just created a project to try the ./gradlew test and it worked.

In mvp_kotlin_example I am using Robolectric for tests and Kotlin for development.

So how can I make my tests work from the command line?

Edit:

This is a open source project, so all the code can be found in the

https://github.com/leandroBorgesFerreira/mvp-kotlin-example

This is my build.gradle file:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply from: '../jacoco.gradle'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "br.com.simplepass.simplepassnew"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled = true
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    testCompile "org.robolectric:robolectric:3.1.4"
    testCompile 'com.squareup.okhttp3:mockwebserver:3.3.1'
    testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
    testCompile "org.mockito:mockito-core:2.4.2"
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile 'br.com.simplepass:loading-button-android:1.5.0'
    compile 'org.jetbrains.anko:anko-sdk15:0.9' // sdk19, sdk21, sdk23 are also available
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.6'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
    compile 'com.google.dagger:dagger:2.8'
    kapt 'com.google.dagger:dagger-compiler:2.8'
    provided 'org.glassfish:javax.annotation:10.0-b28'
}
repositories {
    mavenCentral()
}

kapt {
    generateStubs = true
}

Edit 2:

This is what I see after running ./gradlew tasks (it's a lot less tasks than the usuall)

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'mvp_kotlin_example'.
components - Displays the components produced by root project 'mvp_kotlin_example'. [incubating]
dependencies - Displays all dependencies declared in root project 'mvp_kotlin_example'.
dependencyInsight - Displays the insight into a specific dependency in root project 'mvp_kotlin_example'.
help - Displays a help message.
model - Displays the configuration model of root project 'mvp_kotlin_example'. [incubating]
projects - Displays the sub-projects of root project 'mvp_kotlin_example'.
properties - Displays the properties of root project 'mvp_kotlin_example'.
tasks - Displays the tasks runnable from root project 'mvp_kotlin_example'.

Other tasks
-----------
clean
1
Please show you build.gradle file.Divers
It seems to me that you have androidTest (as opposed to traditional unit tests). For that the command is ./gradlew connectedAndroidTest (or you can use the short form ./gradlew cAT)Budius
Budius, I am not using androidTest. All my tests are in the test/java/[mypackage] and I am trying to use the traditional unit tests. It works in th Android StudioLeandro Borges Ferreira
@LeandroBorgesFerreira what do you see after execution ./gradlew tasks ?Divers
See the edited question. The result is less than the usual.Leandro Borges Ferreira

1 Answers

9
votes

Here the problem has nothing to do with your build.gradle your project was missing the settings.gradle file that tells gradle to include the app folder as a module. I've created PR #1 on your project to resolve this defect, when merged you should be able to run ./gradlew clean test from the root project directory.

https://github.com/leandroBorgesFerreira/mvp-kotlin-example/pull/1

Solution is to just add a oneline settings.gradle file to the root project that declares the modules included in the build.

$ cat settings.gradle 
include 'app'