1. 起因
    1. 在Android studio 刚出。本人就想弄单元测试,可惜当时Android studio不知道抽什么风(准确来说,应该是我不会弄而已)。无法执行到相应的代码。
      后来今天突然自己又抽风。又想去弄一下Android junit。
    2. 本文基于做过Eclipse开发使用过Android junit,如果Eclipse的Android Junit没有使用过,就我没有说过吧!
  2. 准备环境,配置
    1. 官网Demo地址:https://github.com/googlesamples/android-testing-templates.git

    2. 环境
      1. 根据demo中
        1. 单纯想运行java的单元测试就引入

          // Dependencies for local unit tests
          testCompile 'junit:junit:' + rootProject.ext.junitVersion
          testCompile 'org.mockito:mockito-all:' + rootProject.ext.mockitoVersion
          testCompile 'org.hamcrest:hamcrest-all:' + rootProject.ext.hamcrestVersion
        2. 想运行Android的Junit得引入
          // Android Testing Support Library's runner and rules
          androidTestCompile 'com.android.support.test:runner:' + rootProject.ext.runnerVersion
          androidTestCompile 'com.android.support.test:rules:' + rootProject.ext.rulesVersion
      2. 最后在   defaultConfig 节点添加

        defaultConfig {
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        }
      3. 以上配置要配置在“启动项目中”build.gradle文件中,方可自动关联上启动项目
        1. Android Studio本来启动项目中的测试代码分为两个目录
        2. 如果是通过更改“sourceSets”的话。那就要得重新配置注明相应的文件夹(这是Eclipse转AS导出的配置)
          sourceSets{
          sourceSets{
          main{
          java.srcDirs = ['src']
          }
          androidTest{
          java.srcDirs = ['androidTest/src']
          }
          test{
          java.srcDirs = ['test/src']
          }
          }
        3. 最后就把相应的文件放入具体目录
          1. androidTest  “Android的Junit”
          2. test “java的单元测试”
  3. 代码
    1. junit

      public class ExampleUnitTest {
      @Test
      public void addition_isCorrect() throws Exception {
      System.out.println("123"); }
      }
    2. Android junit
      public class AndroidRuntimeCodeTest extends AndroidTestCase {
      
          public void testHello() throws Exception {
      System.out.println("testHello");
      }
      }
  4. 结论
    1. 注意事项
      1. 以前Eclipse做单元测试得往AndroidManifest.xml标明
        1. <manifest >
          <instrumentation
                  android:name="android.test.InstrumentationTestRunner"
                  android:targetPackage="com.example.viewtest" />

          </manifest>
        2. <application>

          <uses-library android:name="android.test.runner" />
          </application>
      2. Android studio就要在build.gradle
        1. 导入相应的包
        2. android{
              defaultConfig {
                  testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
              }
          }
    2. 还有一点:就是“测试使用的需要的包”遇到与“启动项目的包”冲突时,使用
      configurations.all {
      resolutionStrategy {
      androidTestCompile 'com.android.support.test:runner:0.4.1'
      androidTestCompile 'com.android.support.test:rules:0.4.1' forcedModules = ['com.android.support:support-annotations:23.0.1'] }
      }

 

在Android Studio进行“简单配置”单元测试(Android Junit)的更多相关文章

  1. Android studio 安装与配置【Android学习入门】

    终于下定决心认真学习Android开发了. 之前在很多平台看到很多大牛们学习Android的经验和心得,纸上得来终觉浅. 这里推荐stormzhang老师总结的Android学习之路. 为了防止电脑卡 ...

  2. Android Studio开发环境配置以及相关说明

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这里简单记录下在开发的时候使用的Android Studio开发环境版本以及相关注意事项. 一般来讲,每隔一段时间就要检查下Andr ...

  3. Android studio下gradle Robolectric单元测试配置

    android studio下gradle Robolectric单元测试配置 1.Robolectric Robolectric是一个基于junit之上的单元测试框架.它并不依赖于Android提供 ...

  4. Android Studio经常使用配置及使用技巧(二)

    在<Android Studio经常使用配置及使用技巧(一)>中具体描写叙述了Android Studio的project结构和打开开源project的一些配置方法.本篇将从我个人的使用情 ...

  5. Android Studio NDK环境配置

    本文参考:Android Studio NDK环境配置及JNI使用方法:http://blog.csdn.net/tongseng/article/details/53005123 并添加自己的实践: ...

  6. Android Studio一些简单设置

          简单设置   1.默认主题设置 默认的 Android Studio 为灰色界面,能够选择使用炫酷的黑色界面. Settings --> Appearance --> Them ...

  7. 初次使用Android Studio时的配置

    一.第一次安装: Android Studio安装完毕后,第一次启动AS前.为了避免又一次下载新版本号的SDK.操作例如以下: AS启动前.请先将bin文件夹的idea.properties文件里添加 ...

  8. Android Studio开发环境配置(win7)

    由于近期,google网站封锁,导致eclipse配置android环境时,无法在线更新API和配置ADT.个人尝试之后,发现很多地方配置因为版本不合适而配置不成功,(ps:当然你FQ,或者下载到对应 ...

  9. 【转】Android studio安装与配置

    Android studio安装与配置 1.首先下载Android studio安装包,可以从http://www.android-studio.org/下载最新版本,这里采用3.0版本进行演示,对应 ...

随机推荐

  1. 张小龙《微信背后的产品观》之PPT完整文字版

    微信回顾 433天,一亿用户 成为移动互联网的新入口 启动(2010年11月19日) 用户数突破1亿 1.0 1月26日 2.0 5月10日 语音对讲 2.5 8月3日 查看那附近的人 3.0 10月 ...

  2. IntelliJ IDEA 目录技巧

    IntelliJ IDEA的Web应用的目录结构 Ø 目录图: Ø 目录解释: 开发目录 目录名称 描述 Test 工程名称 lib Jar包的存放目录 src 源文件也就是文件(类,资源文件)存放的 ...

  3. web浏览器工作原理

    HTML在浏览器里的渲染原理 我们打开的页面(Web页面)在各种不同的浏览器中运行,浏览器载入.渲染页面的速度直接影响着用户体验,简单地说下页面渲染,页面渲染就是浏览器将html代码根据CSS定义的规 ...

  4. python 多线程 笔记(一)

    #coding=utf-8 import threading from time import sleep, ctime loops = [4,2] def loop(nloop, nsec): pr ...

  5. SQL Server 导入大数据脚本

    .先使用win+r运行cmd .执行osql -S DESKTOP-RGBEDS3 -U sa -P liyang -d FGCPOE_十院 -i D:\数据库\script.sql 即可!

  6. 2.利用NABCD模型进行竞争性需求分析

    1) N (Need 需求) 在宿舍里,舍友下载了一个比较好玩的游戏,一块好看的电影或者共享一个大体积的文件,而你又不想去重新下载,于是乎:‘’哎,win8怎么共享?‘’,‘’我的网上邻居怎么看不到你 ...

  7. 作业七:团队项目——Alpha版本冲刺阶段-02

    昨天进展:框架设计以及菜单设计. 今天安排:完善界面设计以及象棋图片的绘制. 小组一共三人,陈芝航因家里有事,与我们进行了QQ视屏会议.

  8. Wix 安装部署教程(三)自定义安装界面和行为

    接上一篇自定义安装界面,这篇继续探索,首先介绍下,Wix为我们定义了五种风格,每种风格的UI都是有一定顺序的.我们可以改变安装顺序,也可以完全自定义一个Dialog插入其中.比如Wix_Mondo 风 ...

  9. 写给自己看的Linux运维基础(一) - 系统基础

    查看内核版本信息 uname -a 查看发行版本 cat /etc/issue 查看硬件配置 CPU: cat /proc/cpuinfo      more /proc/cpuinfo | grep ...

  10. jenkins2 Jenkinsfile和load

    更复杂的pipeline实例,使用了Jenkinsfile和load. 文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.c ...