3

I am currently running a suite of tests using

adb shell am instrument -w ${PKGNAME}.test/android.support.test.runner.AndroidJUnitRunner

from a bash script. Also, when debugging and writing these tests, I also run them from Android Studio, so I lose the cmd line ability.

What I would like to do is to have a system property or a buildConfig variable that I can set only in my tests, to true, and to be able to use it in my android code.

I can't seem to find a gradle task/config that will set this for this type of test. The only thing I found that was close was testOptions, but this appears to only be for Unit Tests.

Russ Wheeler
  • 2,590
  • 5
  • 30
  • 57

2 Answers2

0

To change some settings only for an androidTest / instrumantation / espresso test I came up with the following solution:

 //DbHelper
 public class DbHelper extends SQLiteOpenHelper {
     public static AtomicBoolean isTestMode = new AtomicBoolean(false);

     private static String getDBName() {
       if (isTestMode.get()){
        return null; // use in memory sqlite db
       } else {
        return DB_NAME;
       }
     }

// within my unit test
@Rule
public ActivityTestRule<MyActivity> mActivityRule = new ActivityTestRule<MyActivity>(
        MyActivity.class){

    @Override
    protected void beforeActivityLaunched() {
        super.beforeActivityLaunched();
        DbHelper.isTestMode.set(true);
    }

    @Override
    protected void afterActivityFinished() {
        super.afterActivityFinished();
        DbHelper.isTestMode.set(false);
    }
};
slowjack2k
  • 2,566
  • 1
  • 15
  • 23
-1

The perfect solution would be to figure out how to avoid having to know in code if you are currently in a test at all. You did not explain why you need this info, so take a look at Comtaler's answer to a similar question. It might be just what you need.

neits
  • 1,963
  • 1
  • 18
  • 32
  • So, it's because I'm struggling with Immersive Mode. I'm using UiAutomator and UiDevice in my tests, but when using Immersive Mode, UiDevice can't get the full screen size. I have buttons I need to click that are under where the navigation menu is. What I want to do in tests, is turn off Immersive mode so my tests will run properly – Russ Wheeler Oct 03 '17 at 09:28
  • Also, I'm not sure if that will help, because when I run Immersive tests, 2 apks are used. My original app, and also a test apk which runs against it. Therefore the app that sets Immersive mode won't have .test. in the package name – Russ Wheeler Oct 03 '17 at 09:29
  • But actually, now that I think about it, I'm not sure if there is anyway for my app to even know if it's being run against the test apk, so maybe this is all impossible anyway... – Russ Wheeler Oct 03 '17 at 09:30
  • Be sure to add that and any other info that might be helpful for people who wish to answer to the original question. What exactly do you mean with "test apk which runs against it"? – neits Oct 05 '17 at 08:48