Android單元測試-對Activity的測試

許佳佳233發表於2017-08-15

前提概要

上一篇文章已經介紹了單元測試的作用和簡單示例,如果不瞭解的讀者可以先閱讀上一篇 Android單元測試-作用以及簡單示例
這篇文章主要介紹常見的Activity中的測試。

對Acitivity的測試

對於Activity,我們大致有兩種測試需求:
1、在Activity正常啟動後,檢視介面佈局是否正確,包括View的點選事件等是否正確。
2、需要在Activity啟動前完成各種資料的部署,然後檢視Activity的效果。

對於這兩種需求,筆者分別做了兩個示例解說:
1、檢測一個佈局中的button和TextView是否正確。
2、從網路動態獲取String到Activity介面顯示,並且這個圖片的URL是由Intent傳遞過來的。

環境部署

首先要匯入espresso-core的包,如下:

dependencies {
    // Other dependencies ...
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

當然在目前的專案架構中一般已經自動匯入了這個包,所以不需要自己匯入,筆者專案中自動匯入的包如下如下:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

}

專案結構如下:
這裡寫圖片描述

佈局View的測試:

package com.example.xujiajia_sx.myexpressotest;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by xujiajia_sx on 2017/8/14.
 */

public class SimpleViewActivity extends Activity{
    private TextView tv;
    private Button btn;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_simple_view);

        initView();
    }

    private void initView() {
        tv=findViewById(R.id.tv_simple_view);
        btn=findViewById(R.id.btn_simple_view);

        tv.setText("111");
        btn.setText("222");

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv.setText("777");
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_simple_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <Button
        android:id="@+id/btn_simple_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>
package com.example.xujiajia_sx.myexpressotest;


import android.support.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
 * Created by xujiajia_sx on 2017/8/14.
 */

public class SimpleViewTest {

    @Rule
    public ActivityTestRule<SimpleViewActivity> mActivityTestRule =
            new ActivityTestRule<SimpleViewActivity>(SimpleViewActivity.class);

    @Test
    public void textViewTest() throws Exception {
        onView(withId(R.id.tv_simple_view))
                .check(matches(withText("111")));
    }

    @Test
    public void buttonTest() throws Exception {
        onView(withId(R.id.btn_simple_view))
                .check(matches(withText("222")))
                .perform(click());
        onView(withId(R.id.tv_simple_view))
                .check(matches(withText("777")));
    }
}

測試主要邏輯:
1、首先要使用ActivityTestRule初始化你要測試的Activity。
2、編寫測試方法,測試View是否是我們預期的樣子。

兩個測試方法邏輯如下:
textViewTest():
在Activity中查詢id為tv_simple_view的View,檢查它的text是否為“111”。
buttonTest():
在Activity中查詢id為btn_simple_view的View,檢查它的text是否為“222”。然後執行點選事件,點選事件的邏輯是在Activity的OnCreate中設定的,是把TextView的text設定為777。在執行完點選事件後,測試方法中繼續測試TextView的text是否為“777”。

讀者可能閱讀到對View的測試非常陌生,不用擔心,此處主要要理解測試的邏輯即可,筆者會在下篇文章具體講解View的各種測試方法。

網路獲取String的Activity測試:

package com.example.xujiajia_sx.myexpressotest;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

/**
 * Created by xujiajia_sx on 2017/8/14.
 */

public class ActTestActivity extends Activity{
    private TextView tv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_act_test);

        initView();
    }

    private void initView() {
        tv= findViewById(R.id.tv_act_test);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url =getIntent().getStringExtra("url");
                final String s=mHttpClient.getInstance().get(url);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv.setText(s);
                    }
                });
            }
        }).start();
    }
}
package com.example.xujiajia_sx.myexpressotest;

/**
 * Created by xujiajia_sx on 2017/8/14.
 */

public class mHttpClient {
    private static HttpURLConnectionClient mClient = null;

    public static void setClient(HttpURLConnectionClient client) {
        mClient = client;
    }

    public static HttpURLConnectionClient getInstance() {
        return mClient;
    }
}
package com.example.xujiajia_sx.myexpressotest;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static android.content.ContentValues.TAG;

/**
 * Created by xujiajia_sx on 2017/8/14.
 */

public class HttpURLConnectionClient {

    public String get(String url) {
        HttpURLConnection conn = null;
        try {
            URL mURL = new URL(url);
            conn = (HttpURLConnection) mURL.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(2000);
            conn.connect();
            InputStream is = conn.getInputStream();
            StringBuilder sb = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            return sb.toString();
        } catch (IOException e) {
            Log.e(TAG, "network error for mini program ", e);
            return "";
        } finally {
            //最後將conn斷開連線
            if (conn != null) {
                conn.disconnect();
            }
        }
    }


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_act_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


</LinearLayout>
package com.example.xujiajia_sx.myexpressotest;

import android.content.Intent;
import android.support.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;

/**
 * Created by xujiajia_sx on 2017/8/14.
 */

public class ActTest {
    @Rule
    public ActivityTestRule<ActTestActivity> mActivityTestRule=
            new ActivityTestRule<ActTestActivity>(ActTestActivity.class){
                @Override
                protected Intent getActivityIntent() {
                    Intent intent=new Intent();
                    intent.putExtra("url","http://www.weather.com.cn/adat/sk/101310201.html");
                    return intent;
                }

                @Override
                protected void beforeActivityLaunched() {
                    mHttpClient.setClient(new HttpURLConnectionClient());
                }
            };

    @Test
    public void mTest() throws Exception{

        Thread.sleep(5000);
    }


}

網路獲取不要忘記在AndroidManifest中加網路許可權喔。

這個Activity的主要邏輯就是接收Intent,然後獲取到傳過來的url,接著通過網路獲取到url的String,顯示到TextView上。

主要測試邏輯:
首先還是要定義ActivityTestRule,確定使用哪個Activity。
與前一個例子不同的是,這裡要重寫ActivityTestRule的兩個方法,getActivityIntent() 和beforeActivityLaunched()。顧名思義,一個是設定Activity獲取到的Intent,另一個是設定Activity啟動跟之前的準備工作。
筆者此處在getActivityIntent() 中設定了傳遞的url,在beforeActivityLaunched()設定的網路獲取的方式。

有些讀者可能會好奇為什麼網路獲取的方式不預設呢,而要通過setClient()來設定?
因為這樣可以更方便我們測試,在正式的專案中,我們可能會需要在程式碼中加入log等操作,但是正式的程式碼一般我們是不會去修改的,但是我們可以繼承它,重寫某些方法,然後把它放到測試需要的地方。
在這裡我們就可以繼承HttpURLConnectionClient 這個類,然後把繼承的子類使用setClient()來作為網路獲取的方式。

總結

Activity的使用方法大致如此了,如果有更多需求的讀者可以檢視一下官方ActivityTestRule的Reference。
連結如下:https://developer.android.google.cn/reference/android/support/test/rule/ActivityTestRule.html
第一種使用方法中設計到了對View的測試,由於篇幅較大,本篇文章未能詳細講述,筆者會在下篇文章做一定講解。

相關文章