博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Android Studio进行“简单配置”单元测试(Android Junit)
阅读量:6988 次
发布时间:2019-06-27

本文共 2415 字,大约阅读时间需要 8 分钟。

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

转载于:https://www.cnblogs.com/shortboy/p/5634006.html

你可能感兴趣的文章
JavaScript语言精粹--replace()与正则
查看>>
linux内存管理---物理地址、线性地址、虚拟地址、逻辑地址之间的转换
查看>>
SQL 设计心得、逗号分隔列表
查看>>
openwrt web管理luci界面修改
查看>>
SQL Server 存储(5/8):理解IAM 页
查看>>
Android 资源保护问题——探索
查看>>
Android File Hierarchy : System Structure Architecture Layout
查看>>
腾讯2014年实习生招聘笔试面试经历
查看>>
把Jar文件转成exe安装文件
查看>>
不浮躁,获取充实感
查看>>
JavaScript中产生标识符方式的演变
查看>>
Pyqt 国际化多语言支持
查看>>
AC自己主动机 总结
查看>>
NuGet的几个小技巧
查看>>
sharepoint 2013 userprofile 用户信息
查看>>
LeetCode——Add Binary
查看>>
从一个activity返回
查看>>
2015第30周五
查看>>
存储在图的形式——邻接列表
查看>>
[CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
查看>>