Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Android testing

I'm new to gradle and Android Studio, and I'm trying to figure out how to run tests. I followed instructions in http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing and I'm able to run Instrument Tests but only executing /gradlew connectedInstrumentTest. I'm having troubles understanding the other check tasks, that don't execute any code in my app. This is what ./gradlew tasks prints

...
Verification tasks
------------------
check - Runs all checks.
connectedCheck - Runs all device checks on currently connected devices.
connectedInstrumentTest - Installs and runs the tests for Build 'Debug' on connected devices.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
...

If check runs all checks... shouldn't it run the ones I get from connectedInstrumentTest?

Also, how can I run tests that don't need the Android environment? Should I place them under /test/java ? If so, what command should I run to execute them?

Sorry if these questions seem very obvious, but I just haven't been able to find any answer to these questions in the docs.

Thank you!

Edit:

So I have made some progress here. It looks like check is not doing anything. It would be (right now) up the developer to add dependant tasks to check to run some JUnit tests. You will need to have to create a task, make it find the sources, compile them and run them.

connectedCheckand connectedInstrumentTest: runs instrumentationTest in the device. (this always worked).

deviceCheck: This is useful, as the docs say, for Continuos integration testing.

like image 955
LocoMike Avatar asked Jun 18 '13 20:06

LocoMike


People also ask

Is Gradle used for testing?

Gradle: IntelliJ IDEA uses Gradle as a default test runner. As an outcome, you get the same test results on the continuous integration (CI) server. Also, tests that are run in the command line will always work in the IDE.

How do you test build Gradle?

End-to-end tests for Gradle plugins stand up a build script, apply the plugin under test and execute the build with a specific task. The outcome of the build (e.g. standard output/error or generated artifacts) verifies the correctness of the functionality.

How do I run a test using Gradle command?

Use the command ./gradlew test to run all tests.


1 Answers

  1. If check runs all checks... shouldn't it run the ones I get from connectedInstrumentTest? you can have to run connectedInstrumentTest task as dependency to check task.

    check.dependsOn connectedInstrumentTest

  2. How can I run tests that don't need the Android environment?

For android projects we can discussed about 3 types of Tests

  1. Junit Test
  2. Robolectric Test
  3. Instrument Test

Junit test

We can't use plain junit test to check android related classes. What we can do is separate core java classes to a java library project and add that dependency to android project.

Robolectric Test

We can use robolectric test to run the unit tests outside of the Emulator. This makes the tests fast and easy to configure with CI servers.

To run robolectric test we use gradle-android-test-plugin It clearly describes how to use that plugin.

Project structure

We have to use default folder structure in order to use this plugin. We have to use folder called 'test' to keep robolectric tests:

 MyProject/
   | settings.gradle
   | build.gradle
   - app/
       | build.gradle
        -main
           -java
              -com.example.calculator    

        -test
           -java
              -com.example.calculator.robolectrictests

build file is

buildscript {
    repositories {
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.6.+'
    classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'
}
apply plugin: 'android'
apply plugin: 'android-test'

repositories {
    mavenCentral()
}
android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"
    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19         
    }
}
dependencies {
    compile 'com.android.support:appcompat-v7:+'   
    testCompile 'junit:junit:4.10'
    testCompile 'org.robolectric:robolectric:2.1.+'
    testCompile 'com.squareup:fest-android:1.0.+'
}

This test task will automatically executes with the check task.

Instrument Test

As you mentioned, this requires using the android emulator. That makes the tests slow, meaning that they're not a good way to do TDD.

We can use robolectric test as unit tests in the TDD process. Instrument test we can use as integrated test in the TDD.

like image 87
Milina Udara Avatar answered Oct 13 '22 00:10

Milina Udara