使用 Espresso 测试文本视图值不为空,如果文本视图值为空,则失败



我是安卓测试的新手。如果用户单击按钮并且 TextView 为空,我正在尝试让测试失败。有没有办法在浓缩咖啡中执行此操作,因此如果 TextView 为空,它将失败。我不想检查文本视图是否包含特定文本,我想检查它是否不为空。

试试这个:

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(""))));
    }
}

仅当文本视图包含任何文本时,测试才会通过。

最新更新