Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually fail an Espresso test

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();
}
like image 400
the_prole Avatar asked Jan 03 '23 12:01

the_prole


1 Answers

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");
}
like image 157
Matthew Avatar answered Jan 06 '23 02:01

Matthew