2017-10-06 5 views
2

New android Testing. 사용자가 단추를 클릭하고 TextView가 비어있는 경우 테스트가 실패하도록하려고합니다. 에스프레소에서이 작업을 수행 할 수있는 방법이 없으므로 TextView가 비어 있으면 오류가 발생합니다. TextView에 특정 텍스트가 들어 있는지 확인하고 싶지 않습니다. 비어 있지 않은지 확인하고 싶습니다.TextView 값이 Espresso를 사용하여 비어 있지 않은지 테스트하고 TextView 값이 비어 있으면 오류가 발생합니다.

답변

1

이를 시도해보십시오 텍스트 뷰가 텍스트가 포함 된 경우

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.isDisplayed; 
import static android.support.test.espresso.matcher.ViewMatchers.withId; 
import static android.support.test.espresso.matcher.ViewMatchers.withText; 
import static org.hamcrest.Matchers.not; 

@RunWith(AndroidJUnit4.class) 
public class MainActivityInstrumentedTest { 

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

    @Test 
    public void checkTextView_isDisplayed_and_notEmpty() throws Exception { 
     // perform a click on the button 
     onView(withId(R.id.button)).perform(click()); 

     // passes if the textView does not match the empty string 
     onView(withId(R.id.textView)).check(matches(not(withText("")))); 
    } 
} 

시험 만 통과합니다.

+0

위의 코드는 textView가 비어있는 경우 테스트를 통과합니다. textView가 비어있을 때 테스트가 실패하도록하고 싶습니다. 나는 그것의 비어있을 때 통과하기를 원하지 않는다. –

+0

내 대답이 업데이트되었습니다. 다시 시도하고 작동하는지 알려주세요. – chornge

+1

예. 위의 편집 된 코드가 작동했습니다. –