zoukankan      html  css  js  c++  java
  • Androidstudio下gradleRobolectric单元测试配置

    Androidstudio下gradleRobolectric单元测试配置 - 好代码编程网

    android studio下gradle Robolectric单元测试配置

    1.Robolectric

    Robolectric另辟蹊径,它并不依赖于Android提供的测试功能,它使用了shadow objects并且运行测试于普通的工作站/服务器JVM,不像模拟器或设备需要dexing(Android dex编译器将类文件编译成Android设备上的Dalvik VM使用的格式),打包,部署和运行的过程,大大减少了测试执行的时间。

    参考:安卓单元测试相关概述http://www.cnblogs.com/droidpilot/archive/2012/04/27/2473291.html

     

    2.下载as插件

    如果是android studio 0.8.9以下的版本,需要按照指示添加额外配置

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

    3.编写gradle 配置

    项目地址:https://github.com/JCAndKSolutions/android-unit-test

    以下是配置的详细说明

     

      1 buildscript {
    2
    3     dependencies {
    4
    5         classpath 'com.android.tools.build:gradle:0.12.+'
    6
    7 //        classpath 'org.robolectric:robolectric-gradle-plugin:0.13.+’
    8
    9 //引用相关gradle插件
    10
    11         classpath 'com.github.jcandksolutions.gradle:android-unit-test:1.6.2'
    12
    13     }
    14
    15 }
    16
    17 apply plugin: 'android-library'
    18
    19 apply plugin: 'idea'
    20
    21 idea {
    22
    23     module {
    24
    25 //设置测试类的输出目录
    26
    27         testOutputDir = file('build/test-classes')
    28
    29     }
    30
    31 }
    32
    33 repositories {
    34
    35     mavenCentral()
    36
    37     maven {
    38
    39         url 'https://oss.sonatype.org/content/repositories/snapshots/'
    40
    41     }
    42
    43 }
    44
    45 //由于android studiode 一个bug,必须把module的iml文件中的android sdk引用放到最下面
    46
    47 task pushDownJdkDependency {
    48
    49 //这里是待测试项目的iml文件名
    50
    51     def imlFile = file("library.iml")
    52
    53     doLast {
    54
    55         try {
    56
    57             def parsedXml = (new XmlParser()).parse(imlFile)
    58
    59             def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
    60
    61
    62
    63             parsedXml.component[1].remove(jdkNode)
    64
    65 //这里是target sdk版本,只需要改数字就行
    66
    67             new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': "Android API 19 Platform", 'jdkType': 'Android SDK'])
    68
    69             def writer = new StringWriter()
    70
    71             new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml)
    72
    73             imlFile.text = writer.toString()
    74
    75
    76
    77         } catch (FileNotFoundException e) {
    78
    79             // nop, iml not found
    80
    81         }
    82
    83     }
    84
    85 }
    86
    87 //在build之前修改iml文件
    88
    89 gradle.projectsEvaluated {
    90
    91     preBuild.dependsOn(pushDownJdkDependency)
    92
    93 }
    94
    95 android {
    96
    97     compileSdkVersion 19
    98
    99     buildToolsVersion '19.1.0'
    100
    101
    102
    103     sourceSets {
    104
    105         main {
    106
    107             manifest.srcFile 'AndroidManifest.xml'
    108
    109             java.srcDirs = ['src/main/java']
    110
    111             res.srcDirs = ['res’]
    112
    113 //指定测试文件所在目录
    114
    115             androidTest.setRoot('src/test')
    116
    117         }
    118
    119     }
    120
    121     defaultConfig {
    122
    123         minSdkVersion 10
    124
    125         targetSdkVersion 19
    126
    127         versionCode 2
    128
    129         versionName "2.0.0"
    130
    131         testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    132
    133     }
    134
    135 }
    136
    137 //应用插件
    138
    139 apply plugin: 'android-unit-test'
    140
    141
    142
    143 dependencies {
    144
    145     compile 'com.android.support:support-v4:19.1.0’
    146
    147 //注意,如果https://github.com/evant/android-studio-unit-test-plugin,此插件没有安装,则可能无法识别testCompile语义
    148
    149 //junit:junit和org.robolectric:robolectric是必须项,其他的项目根据实际引用添加
    150
    151     testCompile 'junit:junit:4.10'
    152
    153     testCompile 'org.robolectric:robolectric:2.3'
    154
    155     testDebugCompile 'org.debugonly.dependency'
    156
    157     testFreeCompile 'Admob.jar'
    158
    159     testCompile 'org.mockito:mockito-all:1.9.5'
    160
    161     testCompile('com.squareup:fest-android:1.0.+') { exclude module: 'support-v4' }
    162
    163 }

    4.运行测试

    直接在as的终端里面执行:gradle test 或者./gradlew test 即可

  • 相关阅读:
    3js深入
    01课js初接触;
    弥合对象/关系之间的鸿沟(一)
    Spiral Matrix——螺旋矩阵
    原来···是不是高手,看try cathch都能看出来···
    Web视频分享处理类库的设计
    每个开发人员现在应该下载的十种必备工具
    使用C#得到局域网内所有主机名,IP地址,MAC地址,使用C# 实现查看所有系统事件
    IIS 错误 :“NETWORK SERVICE does not have write access” 解决办法
    配置Url Remap时发生Parser Error的解决办法
  • 原文地址:https://www.cnblogs.com/seven1979/p/4372526.html
Copyright © 2011-2022 走看看