Skip to content

Testing

Thomas Czogalik edited this page Jan 29, 2017 · 1 revision
  • Try to test everything with Junit tests (because of slow emulator in build server)!
  • If you need to test ui elements, use Roboletric. There is also a tutorial on the site.
  • You can get test coverage on your local machine by starting the 'jacocoTestReport' gradle task.
  • find the task in the gradle task list (or enter 'gradlew jacocoTestReport' into to terminal of Android Studio)
  • the test report will be generated into the 'hrv-band\app\build\reports' directory

Roboletric

  • All Roboletric tests should be in the projects test folder!
  • A good template for testing activity or fragments is the following:
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(RobolectricTestRunner.class)
public class RoboTest {
  @BeforeClass
  public static void beforeClass() {
    //this is optional. Init here variables, but not the activity or fragment!
  }

  @Before
  public void before() {
    //init Activity if it has no fragment in it
    Robolectric.setupActivity(MainActivity.class);
   
    //init android.support.v4.app.Fragment
    SomeFragment fragment = SomeFragment.getInstance();
    SupportFragmentTestUtil.startVisibleFragment(fragment);
  }

  @Test
  public void test() throws Exception {
    ListView listView = (ListView) fragment.getActivity().findViewById(R.id.list);
    assertNotNull(listView);
  }
}
Clone this wiki locally