一、關(guān)于Robolectric3.0
文章中的所有代碼在此:https://github.com/geniusmart/LoveUT ,由于 Robolectric 3.0 和 3.1 版本(包括后續(xù)3.x版本)差異不小,該工程中包含這兩個版本對應(yīng)的測試用例 Demo 。
作為一個軟件開發(fā)攻城獅,無論你多不屑多排斥單元測試,它都是一種非常好的開發(fā)方式,且不談TDD,為自己寫的代碼負(fù)責(zé),測試自己寫的代碼,在自己力所能及的范圍內(nèi)提高產(chǎn)品的質(zhì)量,本是理所當(dāng)然的事情。
那么如何測試自己寫的代碼?點點界面,測測功能固然是一種方式,但是如果能留下一段一勞永逸的測試代碼,讓代碼測試代碼,豈不兩全其美?所以,寫好單元測試,愛惜自己的代碼,愛惜顏值高的QA妹紙,愛惜有價值的產(chǎn)品(沒價值的、政治性的、屁股決定腦袋的產(chǎn)品滾粗),人人有責(zé)!
對于Android app來說,寫起單元測試來瞻前顧后,一方面單元測試需要運行在模擬器上或者真機上,麻煩而且緩慢,另一方面,一些依賴Android SDK的對象(如Activity,TextView等)的測試非常頭疼,Robolectric可以解決此類問題,它的設(shè)計思路便是通過實現(xiàn)一套JVM能運行的Android代碼,從而做到脫離Android環(huán)境進(jìn)行測試。本文對Robolectric3.0做了簡單介紹,并列舉了如何對Android的組件和常見功能進(jìn)行測試的示例。
二、環(huán)境搭建
Gradle配置
在build.gradle中配置如下依賴關(guān)系:
testCompile "org.robolectric:robolectric:3.0"
通過注解配置TestRunner
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class SampleActivityTest {
}
Android Studio的配置
-
在Build Variants面板中,將Test Artifact切換成Unit Tests模式(注:新版本的as已經(jīng)不需要做這項配置),如下圖:
配置Test Artifact working directory 設(shè)置為$MODULE_DIR$
如果在測試過程遇見如下問題,解決的方式就是設(shè)置working directory的值:
java.io.FileNotFoundException: build\intermediates\bundles\debug\AndroidManifest.xml (系統(tǒng)找不到指定的路徑。)
設(shè)置方法如下圖所示:
更多環(huán)境配置可以參考官方網(wǎng)站。
三、Activity的測試
- 創(chuàng)建Activity實例
@Test
public void testActivity() {
SampleActivity sampleActivity = Robolectric.setupActivity(SampleActivity.class);
assertNotNull(sampleActivity);
assertEquals(sampleActivity.getTitle(), "SimpleActivity");
}
- 生命周期
@Test
public void testLifecycle() {
ActivityController<SampleActivity> activityController = Robolectric.buildActivity(SampleActivity.class).create().start();
Activity activity = activityController.get();
TextView textview = (TextView) activity.findViewById(R.id.tv_lifecycle_value);
assertEquals("onCreate",textview.getText().toString());
activityController.resume();
assertEquals("onResume", textview.getText().toString());
activityController.destroy();
assertEquals("onDestroy", textview.getText().toString());
}
- 跳轉(zhuǎn)
@Test
public void testStartActivity() {
//按鈕點擊后跳轉(zhuǎn)到下一個Activity
forwardBtn.performClick();
Intent expectedIntent = new Intent(sampleActivity, LoginActivity.class);
Intent actualIntent = ShadowApplication.getInstance().getNextStartedActivity();
assertEquals(expectedIntent, actualIntent);
}
注:Robolectric 3.1 之后,不建議用 Intent.equals()
的方式來比對兩個 Intent
,因此以上代碼將無法正常執(zhí)行。目前建議用類似代碼來斷言:
assertEquals(expectedIntent.getComponent(), actualIntent.getComponent());
當(dāng)然,Intent
有很多屬性,如果需要分別斷言的話比較麻煩,因此可以用一些第三方庫,比如 assertj-android 的工具類 IntentAssert。
- UI組件狀態(tài)
@Test
public void testViewState(){
CheckBox checkBox = (CheckBox) sampleActivity.findViewById(R.id.checkbox);
Button inverseBtn = (Button) sampleActivity.findViewById(R.id.btn_inverse);
assertTrue(inverseBtn.isEnabled());
checkBox.setChecked(true);
//點擊按鈕,CheckBox反選
inverseBtn.performClick();
assertTrue(!checkBox.isChecked());
inverseBtn.performClick();
assertTrue(checkBox.isChecked());
}
- Dialog
@Test
public void testDialog(){
//點擊按鈕,出現(xiàn)對話框
dialogBtn.performClick();
AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog();
assertNotNull(latestAlertDialog);
}
- Toast
@Test
public void testToast(){
//點擊按鈕,出現(xiàn)吐司
toastBtn.performClick();
assertEquals(ShadowToast.getTextOfLatestToast(),"we love UT");
}
- Fragment的測試
如果使用support的Fragment,需添加以下依賴
testCompile "org.robolectric:shadows-support-v4:3.0"
shadow-support包提供了將Fragment主動添加到Activity中的方法:SupportFragmentTestUtil.startFragment(),簡易的測試代碼如下
@Test
public void testFragment(){
SampleFragment sampleFragment = new SampleFragment();
//此api可以主動添加Fragment到Activity中,因此會觸發(fā)Fragment的onCreateView()
SupportFragmentTestUtil.startFragment(sampleFragment);
assertNotNull(sampleFragment.getView());
}
- 訪問資源文件
@Test
public void testResources() {
Application application = RuntimeEnvironment.application;
String appName = application.getString(R.string.app_name);
String activityTitle = application.getString(R.string.title_activity_simple);
assertEquals("LoveUT", appName);
assertEquals("SimpleActivity",activityTitle);
}
四、BroadcastReceiver的測試
首先看下廣播接收者的代碼
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences.Editor editor = context.getSharedPreferences(
"account", Context.MODE_PRIVATE).edit();
String name = intent.getStringExtra("EXTRA_USERNAME");
editor.putString("USERNAME", name);
editor.apply();
}
}
廣播的測試點可以包含兩個方面,一是應(yīng)用程序是否注冊了該廣播,二是廣播接受者的處理邏輯是否正確,關(guān)于邏輯是否正確,可以直接人為的觸發(fā)onReceive()方法,驗證執(zhí)行后所影響到的數(shù)據(jù)。
@Test
public void testBoradcast(){
ShadowApplication shadowApplication = ShadowApplication.getInstance();
String action = "com.geniusmart.loveut.login";
Intent intent = new Intent(action);
intent.putExtra("EXTRA_USERNAME", "geniusmart");
//測試是否注冊廣播接收者
assertTrue(shadowApplication.hasReceiverForIntent(intent));
//以下測試廣播接受者的處理邏輯是否正確
MyReceiver myReceiver = new MyReceiver();
myReceiver.onReceive(RuntimeEnvironment.application,intent);
SharedPreferences preferences = shadowApplication.getSharedPreferences("account", Context.MODE_PRIVATE);
assertEquals( "geniusmart",preferences.getString("USERNAME", ""));
}
五、Service的測試
Service的測試類似于BroadcastReceiver,以IntentService為例,可以直接觸發(fā)onHandleIntent()方法,用來驗證Service啟動后的邏輯是否正確。
public class SampleIntentService extends IntentService {
public SampleIntentService() {
super("SampleIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(
"example", Context.MODE_PRIVATE).edit();
editor.putString("SAMPLE_DATA", "sample data");
editor.apply();
}
}
以上代碼的單元測試用例:
@Test
public void addsDataToSharedPreference() {
Application application = RuntimeEnvironment.application;
RoboSharedPreferences preferences = (RoboSharedPreferences) application
.getSharedPreferences("example", Context.MODE_PRIVATE);
SampleIntentService registrationService = new SampleIntentService();
registrationService.onHandleIntent(new Intent());
assertEquals(preferences.getString("SAMPLE_DATA", ""), "sample data");
}
六、Shadow的使用
Shadow是Robolectric的立足之本,如其名,作為影子,一定是變幻莫測,時有時無,且依存于本尊。因此,框架針對Android SDK中的對象,提供了很多影子對象(如Activity和ShadowActivity、TextView和ShadowTextView等),這些影子對象,豐富了本尊的行為,能更方便的對Android相關(guān)的對象進(jìn)行測試。
1.使用框架提供的Shadow對象
@Test
public void testDefaultShadow(){
MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
//通過Shadows.shadowOf()可以獲取很多Android對象的Shadow對象
ShadowActivity shadowActivity = Shadows.shadowOf(mainActivity);
ShadowApplication shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application);
Bitmap bitmap = BitmapFactory.decodeFile("Path");
ShadowBitmap shadowBitmap = Shadows.shadowOf(bitmap);
//Shadow對象提供方便我們用于模擬業(yè)務(wù)場景進(jìn)行測試的api
assertNull(shadowActivity.getNextStartedActivity());
assertNull(shadowApplication.getNextStartedActivity());
assertNotNull(shadowBitmap);
}
2.如何自定義Shadow對象
首先,創(chuàng)建原始對象Person
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
其次,創(chuàng)建Person的Shadow對象
@Implements(Person.class)
public class ShadowPerson {
@Implementation
public String getName() {
return "geniusmart";
}
}
接下來,需自定義TestRunner,添加Person對象為要進(jìn)行Shadow的對象(注:Robolectric 3.1 起可以省略此步驟)。
public class CustomShadowTestRunner extends RobolectricGradleTestRunner {
public CustomShadowTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
public InstrumentationConfiguration createClassLoaderConfig() {
InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
/**
* 添加要進(jìn)行Shadow的對象
*/
builder.addInstrumentedPackage(Person.class.getPackage().getName());
builder.addInstrumentedClass(Person.class.getName());
return builder.build();
}
}
最后,在測試用例中,ShadowPerson對象將自動代替原始對象,調(diào)用Shadow對象的數(shù)據(jù)和行為
@RunWith(CustomShadowTestRunner.class)
@Config(constants = BuildConfig.class,shadows = {ShadowPerson.class})
public class ShadowTest {
/**
* 測試自定義的Shadow
*/
@Test
public void testCustomShadow(){
Person person = new Person("genius");
//getName()實際上調(diào)用的是ShadowPerson的方法
assertEquals("geniusmart", person.getName());
//獲取Person對象對應(yīng)的Shadow對象
ShadowPerson shadowPerson = (ShadowPerson) ShadowExtractor.extract(person);
assertEquals("geniusmart", shadowPerson.getName());
}
}
七、關(guān)于代碼
文章中的所有代碼在此:https://github.com/geniusmart/LoveUT
另外,除了文中所示的代碼之外,該工程還包含了Robolectric官方的測試?yán)樱粋€簡單的登錄功能的測試,可以作為入門使用,界面如下圖。
八、參考文章
http://robolectric.org
https://github.com/robolectric/robolectric
http://tech.meituan.com/Android_unit_test.html
關(guān)于代碼中的日志如何輸出、網(wǎng)絡(luò)請求、數(shù)據(jù)庫操作如何測試,請移步第二篇文章Android單元測試框架Robolectric3.0介紹(二)