Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso android -- Evoke assert? (NOT a view assert)

I'm looking to do a simple test. I just want my espresso test script to verify that I'm not on production. Bad things happen if I run a purchase on production, let alone lots them.. I know in Java you need to add a -ae to run assertions. Which doesn't seem to be as simple in an android espresso test. I'll be handing this code off to the testers so I really really need it to fail if it's on the production. (obviously I'll wrap it in an IF, but I want it to be more ugly -- you messed up -- kinda thing.)

public class PurchaseTest  extends BaseFooTest<HomeActivity> //ActivityInstrumentationTestCase2<LoginRegisterActivity>
{

final static String TAG = "PurchaseTest";
static final String PROD_URL = "https://api.foobar.com";
public PurchaseTest()
{
    super(HomeActivity.class);
}

public void test()
{

    System.out.println(fooApplication.hostUrl);
    assert fooApplication.hostUrl.equalsIgnoreCase(PROD_URL) == false;
    assert fooApplication.hostUrl.equalsIgnoreCase(PROD_URL) == true;
//       No assert! Not being read then!

}

////////////////////// boss mans code, that the class is extending, I don't think it matter, but included it incase the extends basefootest confused someone.

   public class BaseFooTest<T extends Activity> extends ActivityInstrumentationTestCase2
   {
   public BaseFooTest( Class<T> activityClass )
   {
       super( activityClass );
   }

   @Override
   public void setUp() throws Exception
   {
        super.setUp();
        getActivity();
        tryClickOk();
    }

    protected ViewAssertion isDisplayed()
    {
        return ViewAssertions.matches( ViewMatchers.isDisplayed() );
    }

    protected void tryClickOk()
    {
        try
        {
            onView( withText( "OK" ) ).perform( click() );
        }
        catch ( NoMatchingViewException e )
        {
        // Eat it
        // System.out.print( e );
        }

    }
}
like image 341
StarWind0 Avatar asked Jan 25 '26 04:01

StarWind0


1 Answers

It's possible to enable keyword asserts, but it requires a manual step, and it would be unusual to use keyword asserts in test code.

It's best to use the JUnit methods (assertTrue, etc.) as you would in unit tests.

like image 114
Daniel Lubarov Avatar answered Jan 27 '26 20:01

Daniel Lubarov