Android Studio under Gradle robolectric Unit test Configuration

Source: Internet
Author: User
Tags sonatype

Android Studio under Gradle robolectric Unit test Configuration

1.Robolectric

Robolectric is a unit test framework based on JUnit. It does not depend on the testing capabilities provided by Android, it uses shadow objects and runs tests on a normal workstation/server JVM, unlike a simulator or device that requires dexing (Android The Dex compiler compiles the class file into the format used by the Dalvik VM on the Android device, and the process of packaging, deploying, and running greatly reduces the time of the test execution.

Reference: Android unit Test related overview http://www.cnblogs.com/droidpilot/archive/2012/04/27/2473291.html

2. Download as Plugin

If you are under Android Studio 0.8.9, you need to follow the instructions to add additional configuration

Https://github.com/evant/android-studio-unit-test-plugin

3. Writing Gradle Configuration

Project address:https://github.com/JCAndKSolutions/android-unit-test

The following is a detailed description of the configuration

Buildscript {repositories {mavencentral () Maven {URL' https://oss.sonatype.org/content/repositories/snapshots/'}} dependencies {Classpath' com.android.tools.build:gradle:0.12.+ '//classpath ' org.robolectric:robolectric-gradle-plugin:0.13.+ '//referencing related Gradle pluginsClasspath ' com.github.jcandksolutions.gradle:android-unit-test:1.6.2 '}}apply Plugin:' Android-library 'Apply plugin:' Idea 'Idea {module {//set the output directory of the test classTestoutputdir = File (' build/test-classes ')}}repositories {mavencentral () Maven {URL' https://oss.sonatype.org/content/repositories/snapshots/'    }}//due to a bug in Android Studiode, you must put the Android SDK reference in the module's IML file to the bottomtask Pushdownjdkdependency {//here is the IML file name for the project under testdef imlfile = File ("Library.iml") Dolast {Try{def parsedxml= (NewXmlparser ()). Parse (imlfile) def Jdknode= parsedxml.component[1].orderentry.find {it. ' @type ' = = ' jdk '} parsedxml.component[1].remove (Jdknode)//This is the target SDK version, just need to change the number.            NewNode (parsedxml.component[1], ' orderentry ', [' type ': ' jdk ', ' jdkname ': ' Android API Platform ', ' jdktype ': ' Android SDK ']) def writer=NewStringWriter ()NewXmlnodeprinter (NewPrintWriter (writer)). Print (parsedxml) Imlfile.text=writer.tostring ()}Catch(FileNotFoundException e) {//NOP, iML not found        }    }}//Modify the iML file before buildgradle.projectsevaluated {Prebuild.dependson (pushdownjdkdependency)}android {compilesdkversion19buildtoolsversion' 19.1.0 'Sourcesets {main {manifest.srcfile' Androidmanifest.xml 'Java.srcdirs= [' Src/main/java '] Res.srcdirs= [' res ']//Specify the directory where the test files are locatedAndroidtest.setroot (' Src/test ')}} defaultconfig {minsdkversion10targetsdkversion19Versioncode2Versionname"2.0.0"Testinstrumentationrunner"Com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"    }}//Application PluginApply plugin: ' Android-unit-test 'Dependencies {Compile' com.android.support:support-v4:19.1.0 '//Note that if https://Github.com/evant/android-studio-unit-test-plugin, testcompile semantics may not be recognized if this plugin is not installed//Junit:junit and org.robolectric:robolectric are required items, other items are added according to the actual referenceTestcompile ' junit:junit:4.10 'Testcompile' org.robolectric:robolectric:2.3 'Testdebugcompile' Org.debugonly.dependency 'Testfreecompile' Admob.jar 'Testcompile' org.mockito:mockito-all:1.9.5 'Testcompile (' com.squareup:fest-android:1.0.+ ') {Exclude module: ' Support-v4 ' }}

4. Run the test

Execute directly inside the AS Terminal: Gradle test or./gradlew test

Android Studio under Gradle robolectric Unit test Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.