こんにちは、KID.Aです。
カバレッジ試験は網羅条件の実行率をみるもので、品質向上の測定や分析に役に立ちます。
今回はAndroid Studioでカバレッジ試験を行う方法を記載します。
実装
まずはgradleファイルに以下の記述を追加します。
defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }
debug { testCoverageEnabled true }
dependencies { androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0' compile 'com.android.support.test:testing-support-lib:0.1' compile 'com.android.support.test.espresso:espresso-core:2.0' }
修正したファイルは全体は以下のようになります。
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { applicationId "jp.co.testapplication" minSdkVersion 23 targetSdkVersion 23 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { debug { testCoverageEnabled true } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } packagingOptions { exclude 'LICENSE.txt' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0' compile 'com.android.support.test:testing-support-lib:0.1' compile 'com.android.support.test.espresso:espresso-core:2.0' }
次にカバレッジ試験をするActivityを作ります。
このActivityはボタンを2つ作っています。
ボタンを押下したらLogが実行されます。
下記はMainActivity.javaの中身になります。
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.v(MainActivity.class.getName(), "click button1"); } }); findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.v(MainActivity.class.getName(), "click button2"); } }); } }
最後にMainActivityの試験用のファイルを作ります。
androidTestのパッケージに以下のMainActivityTest.javaを作成します。
package jp.co.testapplication; import android.support.test.InstrumentationRegistry; import android.support.test.espresso.Espresso; import android.support.test.espresso.action.ViewActions; import android.support.test.espresso.matcher.ViewMatchers; import android.test.ActivityInstrumentationTestCase2; import org.junit.After; import org.junit.Before; import org.junit.Test; import jp.co.testapplication.MainActivity; public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> { public MainActivityTest() { super(MainActivity.class); } @Before public void setUp() throws Exception { super.setUp(); getActivity(); } @After public void tearDown() throws Exception { super.tearDown(); } @Test public void testPressButton() { Espresso.onView(ViewMatchers.withId(R.id.button1)).perform(ViewActions.click()); } }
実行
これから試験を行いカバレッジレポートを作成します。
まずはEdit Configurations…を選択します。
+ボタン > Gradleを選択します。
Gradle Projectに対応のアプリを選びます。
TasksにcreateDebugAndroidTestCoverageReportを記入します。
実行します。実行後、build > reportsにcoverageフォルダが作成されます。
作成されたレポート(html)をみると網羅されている箇所とされていない箇所が色が分かれてでます。
緑の箇所が網羅されている箇所、赤の箇所が網羅されていない箇所になります。
このようにAndroidのカバレッジ試験ができます。