gradlew connectedCheck
コマンドラインから実機テストを行う場合, connectedCheck を行うのが一般的かと思います.
このあたりのコマンドが何をするかは gradlew tasks
で一覧できるので見てみると
- check
- Runs all checks.
- connectedAndroidTest
- Installs and runs instrumentation tests for all flavors on connected devices.
- connectedCheck
- Runs all device checks on currently connected devices.
- connectedDebugAndroidTest
- Installs and runs the tests for debug on connected devices.
- deviceAndroidTest
- Installs and runs instrumentation tests using all Device Providers.
- deviceCheck
- Runs all device checks using Device Providers and Test Servers.
こんな感じになっています.
Junit4を導入してハマる
JUnit4の書き方はデフォルトの書き方とは少し異なります.
大きく分類すると
-
@RunWith
でrunnerを指定 -
@Test
でテストメソッドを指定 - テストメソッド名は何でも良い
といった感じです.
コードを示すと
@RunWith(AndroidJUnit4.class)
public class ApplicationTest {
@Test
public void hogehoge() {
Assert.assertTrue(true);
}
}
こんな感じ.
ただ,gradlew connectedCheckを実行したところ,以下のエラーで失敗しました.
:app2:connectedDebugAndroidTest
com.android.builder.testing.ConnectedDevice > No tests found.[ xxx - 4.4.2] FAILED
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).
:app2:connectedDebugAndroidTest FAILED
No tests found. って言われます.テストあるのに.
デフォルトのコード生成は InstrumentationTestCase
を継承したJunit3形式
この InstrumentationTestCase
の継承を削除しないと,テストが通りませんでした.
うっかりデフォルトのテストコードに @RunWith(AndroidJUnit4.class)
を指定するとハマります.
AndroidStudio上ではエラーにならないのが,またくせ者です.
また, RunWith と InstrumentationTestCase どちらも読み込んでいても,メソッド名が test~
となっていればテストが実行されます.
test
で始まったメソッドで無ければ @Test
アノテーションを付けていても失敗します.
うーん,ややこしい.
時間が無いので殴り書きになってしまった….
@RunWith(AndroidJUnit4.class)
public class ApplicationTest {
@Test
public void hogehoge() {
Assert.assertTrue(true);
}
}
@RunWith(AndroidJUnit4.class)
public class ApplicationTest extends InstrumentationTestCase {
@Test
public void hogehoge() {
Assert.assertTrue(true);
}
}
@RunWith(AndroidJUnit4.class)
public class ApplicationTest extends InstrumentationTestCase {
@Test
public void testHogeHoge() {
Assert.assertTrue(true);
}
}