4

I'm working on android project in Android Studio IDE. Is there any way to create tests for some code in android project that does not require android VM running? I can't mark 'tests' folder as tests and create java files in it. Also i can't see any test-related settings in my module.

Update:

I'm trying the next build.gradle:

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }

    // ...
}

apply plugin: "java"

sourceSets {
    simpleTest {
        java {
            srcDir 'test/java'
        }
        resources {
            srcDir 'test/resources'
        }
        compileClasspath += sourceSets.main.runtimeClasspath
    }
}

task simpleTest(type: Test) {
    description = "Runs simple tests"
    testClassesDir = sourceSets.simpleTest.output.classesDir
    classpath += sourceSets.simpleTest.runtimeClasspath
}

But i'm getting an error:

12:19:10 Gradle 'app' project refresh failed: The 'java' plugin has been applied, but it is not compatible with the Android plugins.

4ntoine
  • 19,816
  • 21
  • 96
  • 220
  • Related: http://stackoverflow.com/questions/21876508/unit-tests-with-android-studio-and-gradle/21883664#21883664 – CJBS Jan 13 '15 at 17:24

2 Answers2

0

User guide for Android Gradle plugin states in http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Simple-build-files that android plugin is incompatible with java plugin (there is only java-base applied for Android projects):

Important: You should only apply the android plugin. Applying the java plugin as well will result in a build error.

Unit testing for Android development is not very well supported by the tools since the beginning and your question has a large overlap with Android project with Robolectric and Gradle (Android studio) Also things frequently changed between Android Studio releases and with Gradle plugin updates.

Community
  • 1
  • 1
Radim
  • 4,721
  • 1
  • 22
  • 25
0

This was challenging at that time, but now it's as easy as 1-2-3 and is regular daily routine: https://developer.android.com/training/testing/unit-testing/local-unit-tests.html

4ntoine
  • 19,816
  • 21
  • 96
  • 220