说明
本文描述的是Junit4,笔者是android 两年的搬运工,写项目很少写单元测试。
但有时候测一段逻辑或者网络请求需要把工程跑一遍,再操作一遍。
接触单元测试才认识到它的强大,它能省去很多麻烦和时间。
我们虽然不是测试,也应该知道怎么去简单使用它来提高效率。
本文的GitHub测试代码地址
一、配置单元测试环境
1,笔者先说两个遇见的问题,按照别人blog说明进行了配置,android studio 给我说 androidTestCast...等等一大堆已经过时了,看着各种各样的测试就烦,简单用下JUnit够用就是了。
一言不和就上图:
图片说明:
1,在config中添加testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”。
2,错误描述,大概意思是测试的api和运行的api版本不同编译失败,
support包下发现有个23.1.1annotations,于是重新指定这个版本就是了
:androidTestCompile ‘com.android.support:support-annotations:25.1.0’
进入正题Junit4
androidTest
创建项目的时候默认生成两个目录,androidTest和test。
如果你也像笔者一样创建的时候把这两个删了也没关系,新建一个包androidTest再加个包java再加路径就是了。
这里要说的是这里目录的测试运行在设备或者模拟器,可以拿到Context和Activity。
在类前指定* @RunWith(AndroidJUnit4.class) *
package unittest.com.cannan.unittest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
private static final String STR="Cannan ";
@Rule
public ActivityTestRule<MainActivity> mainActivityActivityTestRule
=new ActivityTestRule<>(MainActivity.class);
@Test
public void showMsg(){
//onView可能提示不出来导包 可以先写onView();再使用按快捷提示 onView(withId(R.id.editText2)).perform(typeText(STR));
onView(withId(R.id.editText3)).perform(typeText(STR),closeSoftKeyboard());
onView(withId(R.id.button)).perform(click());
Log.i("TAG",getClass().getCanonicalName()+"------------");
}
}
test
test目录下的测试类一般运行与pc上,不能使用Log。
笔者主要使用这里来测试Api或者测试独立java类,灵活运用@before 和@after @test注解就行了,没什么说的。
package com.cqutprint.shundai.api;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.google.gson.Gson;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.functions.Action1;
public class ApiManagerTest {
private ApiServer apiServer;
//@Before 注解方法,运行在最前面,一般做初始化动作。
@Before
public void init() {
System.out.print("--------init----------\n");
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addNetworkInterceptor(new StethoInterceptor());
String BASE_URI = "http://sgn5200.imwork.net:22166/shundai/";
Retrofit.Builder retrofit = new Retrofit.Builder()
.baseUrl(BASE_URI)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(new Gson()));
retrofit.client(builder.build());
apiServer = retrofit.build().create(ApiServer.class);
}
@Test
public void getInstance() throws Exception {
Assert.assertNotNull(apiServer);
}
@Test
public void getGirlData() throws Exception {
setAction(apiServer.getGrilsRx(2));
}
// 测试结束后的动作
@After
public void end(){
System.out.print("\n--------end----------");
}
private void setAction(Observable<ResponseBody> observable){
observable.subscribe(new Action1<ResponseBody>() {
@Override
public void call(ResponseBody responseBody) {
try {
String rs= responseBody.string();
System.out.println(rs);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}