3

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

    }
}
StarWind0
  • 1,554
  • 2
  • 17
  • 46
  • `assertTrue` etc. are normally used in JUnit tests rather than plain java asserts. Any reason you don't want to use them? – Daniel Lubarov Mar 14 '15 at 04:19
  • Nope, the intro to espresso didn't tell me to treat it like jUnit, so thats what I needed to know. Make it an answer and I'll vote it up. – StarWind0 Mar 16 '15 at 16:42

1 Answers1

4

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.

Community
  • 1
  • 1
Daniel Lubarov
  • 7,796
  • 1
  • 37
  • 56
  • Thank you ! I was wondering why, Kotlin, my `assert(condition) { "lazy message" }` would not halt the test when one of them fails !! – Mackovich Jul 07 '20 at 12:54