使用Espresso写测试案例

来源:互联网 发布:瓷砖计算软件 编辑:程序博客网 时间:2024/06/04 01:19

  在Android开发中,测试是一个很少被提及的话题。在早期,Android并没有一个很好 的测试框架,你也很难找到一个测试全面的优秀开源项目。随着Android社区的成熟,出现了诸如Robotium,Robolectric等的 优秀测试框架。而Google也推出了自己的UI测试框架Espresso。

  1.build.gradle中配置Espresso

apply plugin: 'com.android.application' android {    ...    defaultConfig {        ...        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    
    // Always show the result of every unit test, even if it passes.    testOptions.unitTests.all{        testLogging {            events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'        }    }
    packagingOptions {        exclude 'LICENSE.txt'    }} 
dependencies {    ...    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'    androidTestCompile 'com.android.support.test:runner:0.2'}(这里要注意的是,app版本和test版本要一致,比如一个v24.0.0,一个v22.0.0就会报错!!)
2.点击Gradle同步

3.由于我使用的是2.2.3版本的android studio,省去了在Build Variants窗口内的Test Artifact中选择了"Unit Tests"这一步。


点击test新建测试


注意有2个包,我一开始新建到下面报错了,很奇怪,改到上面的目录就没事了。

4.编写例子


5.点击run,开启手机会自己跑,很神奇,第一次跑可能有点慢哦,因为在下什么东西,后面再开就是秒跑啦。


参考链接http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0605/2999.html

原创粉丝点击