• 在Android Studio进行“简单配置”单元测试(Android Junit)


    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']

        }
        }

     
  • 相关阅读:
    3DMAX贴图无法显示
    3DMAX2016安装教程【图文】
    OSG学习:转动的小汽车示例
    JAVA Eclipse 快捷键
    解决JQUERY在IE8,7,6下将字符串转成XML对象时产生的BUG
    毕设二:python 爬取京东的商品评论
    redis 注册为服务
    python 爬取bilibili 视频弹幕
    python 爬取36kr 7x24h快讯
    jQuery实现表格冻结行和列
  • 原文地址:https://www.cnblogs.com/shortboy/p/5634006.html
Copyright © 2020-2023  润新知