Android單元測試框架Robolectric3.0介紹

Guadalupe30 8年前發布 | 59K 次閱讀 單元測試

來自: http://www.jianshu.com/p/9d988a2f8ff7



Robolectric

一、關于Robolectric3.0

作為一個軟件開發攻城獅,無論你多不屑多排斥單元測試,它都是一種非常好的開發方式,且不談TDD,為自己寫的代碼負責,測試自己寫的代碼,在自己力所能及的范圍內提高產品的質量,本是理所當然的事情。

那么如何測試自己寫的代碼?點點界面,測測功能固然是一種方式,但是如果能留下一段一勞永逸的測試代碼,讓代碼測試代碼,豈不兩全其美?所以,寫好單元測試,愛惜自己的代碼,愛惜顏值高的QA妹紙,愛惜有價值的產品(沒價值的、政治性的、屁股決定腦袋的產品滾粗),人人有責!

對于Android app來說,寫起單元測試來瞻前顧后,一方面單元測試需要運行在模擬器上或者真機上,麻煩而且緩慢,另一方面,一些依賴Android SDK的對象(如Activity,TextView等)的測試非常頭疼,Robolectric可以解決此類問題,它的設計思路便是通過實現一套JVM能運行的Android代碼,從而做到脫離Android環境進行測試。本文對Robolectric3.0做了簡單介紹,并列舉了如何對Android的組件和常見功能進行測試的示例。

二、環境搭建

Gradle配置

在build.gradle中配置如下依賴關系:

testCompile "org.robolectric:robolectric:3.0"

通過注解配置TestRunner

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class SampleActivityTest {

}

Android Studio的配置

  1. 在Build Variants面板中,將Test Artifact切換成Unit Tests模式,如下圖:


    配置Test Artifact

    </div> </li>

  2. working directory 設置為$MODULE_DIR$

如果在測試過程遇見如下問題,解決的方式就是設置working directory的值:

java.io.FileNotFoundException: build\intermediates\bundles\debug\AndroidManifest.xml (系統找不到指定的路徑。)

設置方法如下圖所示:


Edit Configurations

Working directory的配置

更多環境配置可以參考官方網站

三、Activity的測試

  1. 創建Activity實例
    @Test
    public void testActivity() {
         SampleActivity sampleActivity = Robolectric.setupActivity(SampleActivity.class);
         assertNotNull(sampleActivity);
         assertEquals(sampleActivity.getTitle(), "SimpleActivity");
     }
  2. 生命周期
    @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());
     }
  3. 跳轉
    @Test
    public void testStartActivity() {
         //按鈕點擊后跳轉到下一個Activity
         forwardBtn.performClick();
         Intent expectedIntent = new Intent(sampleActivity, LoginActivity.class);
         Intent actualIntent = ShadowApplication.getInstance().getNextStartedActivity();
         assertEquals(expectedIntent, actualIntent);
     }
  4. UI組件狀態

    @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());
     }
  5. Dialog
    @Test
    public void testDialog(){
         //點擊按鈕,出現對話框
         dialogBtn.performClick();
         AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog();
         assertNotNull(latestAlertDialog);
     }
  6. Toast
    @Test
    public void testToast(){
         //點擊按鈕,出現吐司
         toastBtn.performClick();
         assertEquals(ShadowToast.getTextOfLatestToast(),"we love UT");
     }
  7. 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中,因此會觸發Fragment的onCreateView()
     SupportFragmentTestUtil.startFragment(sampleFragment);
     assertNotNull(sampleFragment.getView());
    }
  8. 訪問資源文件
    @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);
     }
  9. </ol>

    四、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();
        }
    }

    廣播的測試點可以包含兩個方面,一是應用程序是否注冊了該廣播,二是廣播接受者的處理邏輯是否正確,關于邏輯是否正確,可以直接人為的觸發onReceive()方法,驗證執行后所影響到的數據。

    @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", ""));
    }</pre> <h2>五、Service的測試</h2>
    

    Service的測試類似于BroadcastReceiver,以IntentService為例,可以直接觸發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相關的對象進行測試。

    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對象提供方便我們用于模擬業務場景進行測試的api
        assertNull(shadowActivity.getNextStartedActivity());
        assertNull(shadowApplication.getNextStartedActivity());
        assertNotNull(shadowBitmap);
    
    }

    2.如何自定義Shadow對象

    首先,創建原始對象Person

    public class Person {
        private String name;
        public Person(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
    }

    其次,創建Person的Shadow對象

    @Implements(Person.class)
    public class ShadowPerson {
    
        @Implementation
        public String getName() {
            return "geniusmart";
        }
    }

    接下來,需自定義TestRunner,添加Person對象為要進行Shadow的對象

    public class CustomShadowTestRunner extends RobolectricGradleTestRunner {
    
        public CustomShadowTestRunner(Class<?> klass) throws InitializationError {
            super(klass);
        }
    
        @Override
        public InstrumentationConfiguration createClassLoaderConfig() {
            InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
            /**
             * 添加要進行Shadow的對象
             */
            builder.addInstrumentedClass(Person.class.getName());
            return builder.build();
        }
    }

    最后,在測試用例中,ShadowPerson對象將自動代替原始對象,調用Shadow對象的數據和行為

    @RunWith(CustomShadowTestRunner.class)
    @Config(constants = BuildConfig.class,shadows = {ShadowPerson.class})
    public class ShadowTest {
    
        /**
         * 測試自定義的Shadow
         */
        @Test
        public void testCustomShadow(){
            Person person = new Person("genius");
            //getName()實際上調用的是ShadowPerson的方法
            assertEquals("geniusmart", person.getName());
    
            //獲取Person對象對應的Shadow對象
            ShadowPerson shadowPerson = (ShadowPerson) ShadowExtractor.extract(person);
            assertEquals("geniusmart", shadowPerson.getName());
        }
    }

    七、關于代碼

    文章中的所有代碼在此:https://github.com/geniusmart/LoveUT
    另外,除了文中所示的代碼之外,該工程還包含了Robolectric官方的測試例子,一個簡單的登錄功能的測試,可以作為入門使用,界面如下圖。


    官方的登錄測試DEMO

    八、參考文章

    http://robolectric.org
    https://github.com/robolectric/robolectric
    http://tech.meituan.com/Android_unit_test.html

 本文由用戶 Guadalupe30 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!