比想象中好用的Android单元测试——初探
来源:互联网 发布:java 8 in action pdf 编辑:程序博客网 时间:2024/05/23 20:52
依稀记得还在学校学习JAVA的时候,那时候JUnit提供了很好用的单元测试体验。然而在实际工作开发android快要两年了,还没有试过android是否也可以进行相似的单元测试从而避免每次一次写完功能之后都要花几分钟去RUN一遍。所以今天就来探究一下Android的单元测试。
Android单元测试
Android的单元测试有两种方式:本地单元测试和设备单元测试,本地单元测试可以直接运行在本地机器上面的Java Virtual Machine(JVM)上面。它的特点是运行时间短,执行效率高,但是依赖Android framework,每个文件都可以进行单独的测试。
运行在设备上的单元测试,运行的是一个单独的APK,直接运行到虚拟机或者设备上,这种单元测试拥有整个App的生命周期和运行环境。
今天我们就来尝试一下进行这两种测试。
在android studio中新建项目的话,会自动为你创建测试目录(如上图)。
本地单元测试
创建一个普通的工具类来试一试吧。
public class StringUtil { /** * @Date: 2016年7月10日14:48:34 * @return : 判断两个String是否相同 */ public static boolean isSameString(String strSource, String strTarget) { // 都不为null if (null != strSource && null != strTarget) { return strSource.equals(strTarget); } // 两个都为null else if (null == strSource && null == strTarget) { return true; } else // 有一个为null { return false; } }}
在test目录下创建一个Test类
public class StringUtilTest { @Test public void testIsSameString(){ Assert.assertFalse(StringUtil.isSameString(null,"123")); Assert.assertTrue(StringUtil.isSameString("123","123")); }}
运行目录下所有测试
或者运行单个测试文件
如果发现测试文件无法运行,需要将测试模式改为Unit Tests:
设备单元测试
1.先将上面提到的测试模式改为 Android Instrumentation Tests。
2.并在AndroidManifest.xml中application标签之前加入
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="包名" android:label="test"/>
为了使测试应用与原应用运行在同一进程,请填写原应用包名。
3.在application标签中加入
<uses-library android:name="android.test.runner"/>
例子:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ysdemo.empty"> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.ysdemo.empty" android:label="test"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="android.test.runner"/> </application></manifest>
4.依然测试刚才的StringUtil.java,在androidtest目录下创建StringUtilTest.java:
public class StringUtilTest extends AndroidTestCase { public void testIsSameString(){ Assert.assertFalse(StringUtil.isSameString(null,"123")); Assert.assertTrue(StringUtil.isSameString("123","123")); Log.i("YS!!","unit test!!"); }}
5.连接真机/虚拟机,进行设备单元测试
与本地单元测试基本相同,同时可以看到在Android Monitor里打出了log。
总结
android stduio自带的单元测试功能已经很强大,而且也简单易上手,可以考虑自己每每在写完一些重要功能之后先进行单元测试之后,再提交代码交付测试啦~
下回预告:android单元测试第三方框架的使用
- 比想象中好用的Android单元测试——初探
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——JunitTest
- Android单元测试初探——Instrumentation
- android单元测试初探
- 希望比他们想象的还要多
- 算法比我们想象的重要得多
- 翻译的进度比想象中快
- 网站优化远比想象的难
- 单元测试初探 — 使用Stub快速反馈
- 【安卓篇】Android单元测试初探Instrumentation
- “你的胸怀远比想象中的要大”
- 程序员面试金典题解 双栈排序
- Hadoop实战:MapReduce应用实例
- UIView的layoutSubviews和drawRect
- Spring Security(19)——对Acl的支持
- python学习笔记(一)
- 比想象中好用的Android单元测试——初探
- 支持向量机(四)
- ASP.NET页面之间传递值的几种方式
- 安装BEPL
- hdu 5087 Revenge of LIS II(LIS,思路)
- Spring Security(20)——整合Cas
- KMP算法深入理解
- C++的类型转换
- vs2010中配置OpenGL