Is there a way to manually fail an Espresso test?
Say I want to compare the values of two int
types and fail the test if they don't match:
@Test
public void testTwoInts() {
int var1 = mActivityTestRule.getActivity().var1;
int var1 = mActivityTestRule.getActivity().var2;
if(var1 != var2)
fail();
}
Espresso is built on JUnit, so you can use JUnit assertions with it. This makes it much clearer what you are intending to do. In this case, do import static org.junit.Assert.assertTrue
and then in your test, do
@Test
public void testTwoInts() {
int var1 = mActivityTestRule.getActivity().var1;
int var1 = mActivityTestRule.getActivity().var2;
assertEquals(var1,var2);
}
If you really want to do it manually (not recommended), JUnit provides a fail method in that same class. Do import static org.junit.Assert.fail
and then use either version of the fail method (one takes no arguments, and one takes a string explaining the reason for the failure). Here you could do
@Test
public void testTwoInts() {
int var1 = mActivityTestRule.getActivity().var1;
int var1 = mActivityTestRule.getActivity().var2;
if(var1 != var2)
fail("The integers are not equal");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With