How to run unit tests with Android Studio

二次信任 提交于 2019-11-27 19:02:43

just add folder named instrumentTest under /src it should have /java inside like this

then extend the class ActivityTestCase (or any other android unit-test-class), such as

package com.example.app.test;

import android.test.ActivityTestCase;

import junit.framework.Assert;


public class MainActivityTest extends ActivityTestCase {


public void testHappy(){
    Assert.assertTrue(true);
}

} 

right click on green java directory and select run all tests and you should get this:

good luck

Update for AS 1.1+, android gradle plugin 1.1+

Finally it is possible without many tricks. Here is example of project that shows how to setup Robolectric test in Android Studio v1.1+ and android gradle plugin v1.1+: https://github.com/nenick/AndroidStudioAndRobolectric

You can find also there possible issue and workarounds. Yes, Robolectric is complex and not officially supported by Google so it still has some issues. But most of the time it works and brings huge value to your project.

I would also encourage you to start using Robolectric v3+. It is almost released and stable enough.

Old answer for AS 0.x and 1.0x and android gradle plugin version below 1.1

I managed to make it with help of friends.

So basically you need to make next changes to run Robolectric unit tests in Android Studio:

  1. Copy your classpath for test (you can find it as first line in "Run" log)
  2. Open run configuration for your unit tests
  3. Change working dir to folder where AndroidManifest.xml is present
  4. Add VM Option -classpath "<path_to_project_folder>/build/test-classes:<path_to_gradle_cache>/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar:<your old classpath>"

As for me the start of new classpath looks like this:

/Users/emartynov/Development/Projects/work/android.project/build/test-classes:/Users/emartynov/.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar

Problems:

  1. You can run test only for debug variant
  2. Every new test run configuration requires such manual changes. But this is simply copy/paste of two edit fields

I have Android Studio 0.6 version. Here is again part of my build.gradle file:

buildscript {
  repositories {
    mavenCentral()
    maven { url 'https://github.com/rockerhieu/mvn-repo/raw/master/' }
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:0.11.+'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.3'
  //  classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.1'
    classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.1-SNAPSHOT'
    classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.10.+'
  }
}

apply plugin: 'android-sdk-manager'
apply plugin: 'android'
apply plugin: 'android-apt'
apply plugin: 'android-test'

repositories {
  mavenCentral()
}

android {
  compileSdkVersion 19
  buildToolsVersion "19.1.0"

  packagingOptions {
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/ASL2.0'
    exclude 'LICENSE.txt'
  }

  defaultConfig {
    minSdkVersion 14
    targetSdkVersion 19
    versionCode 1
    versionName "0.9.0"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
  }

  sourceSets {
    androidTest.setRoot( 'src/test' )
  }
}

dependencies {
  // butter knife
  compile 'com.jakewharton:butterknife:5.0.0'
  // dagger
  compile 'com.squareup.dagger:dagger:1.2.1'

  // apt
  apt 'com.squareup.dagger:dagger-compiler:1.+'

  // AS tests
  androidTestCompile 'junit:junit:4.+'
  androidTestCompile( 'org.robolectric:robolectric:2.3' ) {
    exclude group: 'commons-logging'
    exclude group: 'org.apache.httpcomponents'
  }
  androidTestCompile 'com.squareup:fest-android:1.+'
  androidTestCompile 'org.mockito:mockito-all:1.9.+'
  androidTestCompile 'org.easytesting:fest-assert-core:2.0M10'
  androidTestCompile( 'org.skyscreamer:jsonassert:1.2.+' ) {
    exclude group: 'org.json'
  }

  // tests
  testCompile 'junit:junit:4.+'
  testCompile( 'org.robolectric:robolectric:2.3' ) {
    exclude group: 'commons-logging'
    exclude group: 'org.apache.httpcomponents'
  }
  testCompile 'com.squareup:fest-android:1.+'
  testCompile 'org.mockito:mockito-all:1.9.+'
  testCompile 'org.easytesting:fest-assert-core:2.0M10'
  testCompile 'com.squareup.dagger:dagger-compiler:1.+'
  testCompile( 'org.skyscreamer:jsonassert:1.2.+' ) {
    exclude group: 'org.json'
  }
}

I ran into this problem and found a solution - include the classes.jar from the exploded bundle (.aar) in the build folder. I don't think will help with finding resources in .aar dependencies though.

testCompile fileTree(dir: "$project.buildDir/exploded-bundles", include: "**/classes.jar")

Edit: Since Android Gradle build tools 0.9.0 the dependency has changed to:

androidTestCompile fileTree(dir: "$project.buildDir/exploded-aar", include: "**/classes.jar")

Edit 2: Since Android Gradle build tools 0.10.0 the dependency has changed to:

androidTestCompile fileTree(dir: "$project.buildDir/../../build/exploded-aar", include: "**/classes.jar")

Note: the relative path may be different depending on your project structure.

I had a similar problem with AS 1.2.2.

I followed the steps here. Basically:

  • Opened the "Build Variants" tool window (see image on the link) and changed the "Test Artifact" drop-down to "Unit tests".
  • Create a directory for your testing source code, i.e. src/test/java, and move your test to the respective package there.
  • Make sure the following sections of your build.gradle file contain these:

    dependencies { 
        testCompile 'junit:junit:4.12'
    }
    
    android {
        sourceSets {
            test {
                resources {
                    srcDir "test"
                }
            }
        }
    }
    

Voila! Right-click your test case and select the JUnit flavor.

BTW, it seems to toggle the visibility of the JUnit/Android tests when you change the "Build Variants" tool, so my guess is you can either test as JUnit or Android but not both at same time.

scottyab

For posterity Android Studio 2.0+ supports running Unit tests without plugins.

This screen can be accessed through menu Run > Edit Configurations...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!