Espresso를 이용한 UITest를 작성 중 ProgressBar의 값이 제대로 설정되었는지 확인하기 위한 테스트를 만들기 위해 검색을 했었다.
공식 문서에는 일반 View 예제가 주로 설명이 되어있고 마지막 줄에 CustomView 관련해서 Attribute를 확인할 수 있는 코드가 링크가 되어있었다.
- Espresso 기초 : https://developer.android.com/training/testing/espresso/basics
Espresso basics | Android Developers
This document explains how to complete common automated testing tasks using the Espresso API. The Espresso API encourages test authors to think in terms of what a user might do while interacting with the application - locating UI elements and interacting w
developer.android.com
- Espresso CustomView : https://github.com/googlesamples/android-testing/tree/master/ui/espresso/CustomMatcherSample
googlesamples/android-testing
A collection of samples demonstrating different frameworks and techniques for automated testing - googlesamples/android-testing
github.com
- Espresso RecyclerView : https://github.com/googlesamples/android-testing/tree/master/ui/espresso/RecyclerViewSample
googlesamples/android-testing
A collection of samples demonstrating different frameworks and techniques for automated testing - googlesamples/android-testing
github.com
그 중 Espresso CustomView를 참조해서 ProgressBar의 값을 확인할 수 있는 CustomMatcher를 만들어봤다. 바로 밑에 코드는 프로그레스를 확인하는 코드이고 그 밑에는 해당 Matcher를 이용한 테스트 코드이다.
public class ProgressMatcher {
static Matcher<View> withProgress(final int progress) {
return withProgress(is(progress));
}
static Matcher<View> withProgress(final Matcher<Integer> integerMatcher) {
checkNotNull(integerMatcher);
return new BoundedMatcher<View, ProgressBar>(ProgressBar.class) {
@Override
protected boolean matchesSafely(ProgressBar item) {
final int progress = item.getProgress();
return integerMatcher.matches(progress);
}
@Override
public void describeTo(Description description) {
}
};
}
}
@Test
public void splash_DisplayProgress() throws Exception {
// Given
// When
mSplashActivityTestRule.getActivity().showProgress(50, 100);
// Then
onView(withId(R.id.activity_splash_loading_pb))
.check(matches(isDisplayed()))
.check(matches(ProgressMatcher.withProgress(50)));
}
끝
'Android > Test' 카테고리의 다른 글
[Android] Android Testing Codelab(Java 버전) (0) | 2019.06.05 |
---|