首先,Junit单元测试要实现的功能,就是用来测试写好的方法是否能够正确的执行,一般多用于对业务方法的测试。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.junittest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme" >
<!-- 引入单元测试的库 -->
<uses-library android:name="android.test.runner" />
</application>
<!-- 配置包名 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.junittest" >
</instrumentation>
</manifest>android:targetPackage指的的要测试的项目的包名,其实就是当前项目的包名即可
public class Demo {
// 要测试的方法,我们设置返回值为10
public int getNum() {
return 10;
}
}
import android.test.AndroidTestCase;
/**
* 单元测试类
*
* @author zhaokaiqiang
*
*/
public class Test extends AndroidTestCase {
// 用此方法对需要测试的方法进行测试,一定要抛出Exception,这样如果出现异常,Junit测试框架才能作出反应
public void t() throws Exception {
int i = new Demo().getNum();
// 这是assert断言的使用,其实就是我们认为返回结果应该为9,但是返回的其实是10,所以这句话肯定会抛异常
assertEquals(9, i);
}
}
【Android进阶】Junit单元测试环境搭建以及简单实用,布布扣,bubuko.com
【Android进阶】Junit单元测试环境搭建以及简单实用
原文:http://blog.csdn.net/bz419927089/article/details/28101769