1.介绍
如今的android studio 3.01开发工具已经非常强大了,当我们创建工程的时候就默认帮我们创建了android和java两个单元测试模板,
我们只需要按照格式进行编写就可以方便的使用了.
为啥要用单元测试?写程序的时候我们每写完一个功能需要编译和运行,并且安装应用到机子上,即便这个功能并不需要让机子跑起来.
我只想知道这个功能的逻辑是否正确.
android的测试模板:可以拿到android的相关的内容,比如Context等
java的测试模板:测试纯java的代码功能和逻辑.
比如我们写了一个类:
package com.android.myapplication;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
public class ReadContantsEngine {
//读取手机联系人
public static void readContants(Context context) {
Log.e("===","===="+context.getPackageName());
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.parse("content://com.android.contacts/contacts");
Uri uridatas = Uri.parse("content://com.android.contacts/data");
Cursor cursor = contentResolver.query(uri, new String[]{"_id"}, null, null, null);
//循环取出数据
while (cursor.moveToNext()){
System.out.println(cursor.getString(0));
String id = cursor.getString(0);//获取到联系人的id
Cursor cursor1 = contentResolver.query(uridatas, new String[]{"data1", "mimetype"}, "raw_contact_id = ?", new String[]{id}, null);
while (cursor1.moveToNext()){
String data = cursor1.getString(0);
String mimetype = cursor1.getString(1);
if (mimetype.equals("vnd.android.cursor.item/name")){
System.out.println("第"+id+"个用户 名字:"+data);
} else if (mimetype.equals("vnd.android.cursor.item/phone_v2")){
System.out.println("第"+id+"个用户 电话:"+data);
}
}
cursor1.close();
}
cursor.close();
}
public static int readMaxNumber(int i, int i1) {
return i>i1?i:i1;
}
}
显然第一个方法需要上下文对象,若要单元测试需要用android的测试模板实现,第二个方法不需要上下文,则两个模板都可以使用,因为不需要android相关的环境:
package com.android.myapplication;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.android.myapplication", appContext.getPackageName());
}
@Test
public void readContants() throws Exception {
ReadContantsEngine.readContants(InstrumentationRegistry.getTargetContext());
}
@Test
public void readMaxValue() throws Exception {
int max = ReadContantsEngine.readMaxNumber(1, 10);
Assert.assertEquals(1,max);
}
}
结果log输出:
01-12 10:49:33.691 3863-3876/com.android.myapplication E/===: ====com.android.myapplication
01-12 10:49:33.701 3863-3876/com.android.myapplication I/System.out: 1
01-12 10:49:33.701 3863-3876/com.android.myapplication I/System.out: 第1个用户 电话:1111-111-11
01-12 10:49:33.701 3863-3876/com.android.myapplication I/System.out: 第1个用户 名字:Aa
01-12 10:49:33.721 3863-3876/com.android.myapplication I/System.out: 2
01-12 10:49:33.731 3863-3876/com.android.myapplication I/System.out: 第2个用户 电话:778-8888
01-12 10:49:33.731 3863-3876/com.android.myapplication I/System.out: 第2个用户 名字:Bsdg
01-12 10:49:33.731 3863-3876/com.android.myapplication I/System.out: 3
01-12 10:49:33.741 3863-3876/com.android.myapplication I/System.out: 第3个用户 电话:1 352-356-2457
01-12 10:49:33.741 3863-3876/com.android.myapplication I/System.out: 第3个用户 名字:Zff
01-12 10:49:33.741 3863-3876/com.android.myapplication I/TestRunner: finished: readContants(com.android.myapplication.ExampleInstrumentedTest)
java单元测试模板:
package com.android.myapplication;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
@Test
public void readMaxValue() throws Exception {
int max = ReadContantsEngine.readMaxNumber(1, 10);
assertEquals(10,max);
}
}
输出,显示绿条表示成功.红条表示失败,并打印失败信息.
还有比较方便的是,当你写完一个方法的时候,鼠标点击方法名,右键选择Goto-->test可直接生成测试类.