您的位置:首页 > 移动开发 > Android开发

Android开发的测试功能的开发文档学习--介绍

2016-02-25 14:29 537 查看
在开发的过程中,我们一直都会忽略的一个步骤,就是在开发的过程中的功能测试,那是因为我们不知道开发过程中的功能测试的重要性。或许有的人会回答,每当完成一个功能的时候,我都会在真机上运行测试的啊,但是这样的效率真的很低,养成良好的开发习惯,可以提高开发效率。其实,Google官方文档已经为我们提供了相当专业、有效率的API。
Testing allows you to verify the correctness, functional behavior, and usability of your app before it is released publicly.
测试能够让应用程序在发布之前验证程序的正确性,功能性以及它的可用性。


**

一、分类

**

测试的方式分为两种:Local Unit Tests和Instrumented Unit Tests。两者之间的不同表现在这几个方面:

第一:运行环境不同

Local Unit Tests是运行在本地的JVM上,而Instrumented Unit Tests既可以运行在物理设备上,也可以运行在虚拟机上。

第二:用例运行的Test Artifact不同

Local Unit Tests的Artifact是Unit Tests;而Intrusmened Unit Test的artifact是Android Intrusmentation Tests。

第三:用例的代码所处的位置不同

在android studio中,我们选择project的模式为android,在java文件夹里面会有两个文件,一个就是我们的java代码所在的文件夹,而另一个就是我们测试用例的代码所在的文件夹。

当我们点击android studio的Build variants来调整Test Artifact的模式的时候,我们会发现用例所在的文件夹的文件名括号里面的变化

1、选择了Unit Tests的话,文件名的括号里面的内容会变成(Test),这就表明我们此时创建的用例是Local Unit Tests

2、选择的是android Instrumentation Test的话,文件名的括号里面的内容会变成(androidTest),这就表明我们此时创建的用例是android Instrumented Test。

**

二、build.gradle环境配置

**

Local Unit Test

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
compile 'com.android.support:appcompat-v7:23.1.1'
}


Android Instrumened Unit Test -Espresso

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
abortOnError false
}
productFlavors {
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
// App dependencies
compile 'com.android.support:support-annotations:23.0.1'
compile 'com.google.guava:guava:18.0'
// Testing-only dependencies
// Force usage of support annotations in the test app, since it is internally used by the runner module.
androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
testCompile 'junit:junit:4.12'
}


Android Instrumened Unit Test -UiAutomater

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile 'com.google.guava:guava:18.0'
testCompile 'junit:junit:4.12'

androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}


三 测试对象

Local Unite Test是运行在本地的,所以主要用来测试创建类的方法的正确性;

android Instrumentation Test可以运行在物理设备上和虚拟机上,所以多用来测试UI。

后面的文章我会针对两种不同的测试进行详细的总结。以上都是自己的理解,欢迎各位同行的纠正指导。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息