2
我是android测试的新手。我试图让测试失败,如果用户点击一个按钮,并且TextView为空。有没有办法在espresso中做到这一点,所以如果TextView为空,它会失败。我不想检查TextView包含特定的文本,我想检查它不是空的。使用Espresso测试TextView值不是空的,如果TextView值为空,则测试失败
我是android测试的新手。我试图让测试失败,如果用户点击一个按钮,并且TextView为空。有没有办法在espresso中做到这一点,所以如果TextView为空,它会失败。我不想检查TextView包含特定的文本,我想检查它不是空的。使用Espresso测试TextView值不是空的,如果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(""))));
}
}
测试将只通过。
当textView为空时,上面的代码将使测试通过。当textView为空时,我想让测试失败。当它空着时,我不希望它通过。 –
我已更新我的答案,再试一次,让我知道它是否有效 – chornge
是上述编辑的代码工作。 –