How to detect whether android app is running UI test with Espresso

后端 未结 8 1925
-上瘾入骨i
-上瘾入骨i 2020-12-03 06:56

I am writing some Espresso tests for Android. I am running in the the following problem:

In order for a certain test case to run properly, I need to disable some fea

相关标签:
8条回答
  • 2020-12-03 07:29

    I'll create two files like below

    src/main/.../Injection.java

    src/androidTest/.../Injection.java

    And in Injection.java I'll use different implementation, or just a static variable int it.

    Since androidTest is the source set, not a part of build type, I think what you want to do is hard.

    0 讨论(0)
  • 2020-12-03 07:34

    If you are using JitPack with kotlin. You need to change Espresso's package name .

    val isRunningTest : Boolean by lazy {
        try {
            Class.forName("androidx.test.espresso.Espresso")
            true
        } catch (e: ClassNotFoundException) {
            false
        }
    }
    

    For checking

    if (isRunningTest) {
      // Espresso only code
    }
    
    0 讨论(0)
  • 2020-12-03 07:35

    Combining Commonsware comment + Comtaler's solution here's a way to do it for any test class using the Espresso framework.

    public static synchronized boolean isRunningTest () {
            if (null == isRunningTest) {
                boolean istest;
    
                try {
                    Class.forName ("android.support.test.espresso.Espresso");
                    istest = true;
                } catch (ClassNotFoundException e) {
                    istest = false;
                }
    
                isRunningTest = new AtomicBoolean (istest);
            }
    
            return isRunningTest.get();
        }
    
    0 讨论(0)
  • 2020-12-03 07:36

    You can use SharedPreferences for this.

    Set debug mode:

    boolean isDebug = true;
    
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("DEBUG_MODE", isDebug);
    editor.commit();
    

    Check if debug mode:

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    boolean isDebug = sharedPref.getBoolean("DEBUG_MODE", false);
    
    if(isDebug){
        //Activate debug features
    }else{
        //Disable debug features
    }
    
    0 讨论(0)
  • 2020-12-03 07:40

    How about a flag in BuildConfig class?

    android {
        defaultConfig {
            // No automatic import :(
            buildConfigField "java.util.concurrent.atomic.AtomicBoolean", "IS_TESTING", "new java.util.concurrent.atomic.AtomicBoolean(false)"
        }
    }
    

    Add this somewhere in your test classes.

    static {
        BuildConfig.IS_TESTING.set(true);
    }
    
    0 讨论(0)
  • 2020-12-03 07:42

    Combined with CommonsWare's answer. Here is my solution:

    I defined an AtomicBoolean variable and a function to check whether it's running test:

    private AtomicBoolean isRunningTest;
    
    public synchronized boolean isRunningTest () {
        if (null == isRunningTest) {
            boolean istest;
    
            try {
                Class.forName ("myApp.package.name.test.class.name");
                istest = true;
            } catch (ClassNotFoundException e) {
                istest = false;
            }
    
            isRunningTest = new AtomicBoolean (istest);
        }
    
        return isRunningTest.get ();
    }
    

    This avoids doing the try-catch check every time you need to check the value and it only runs the check the first time you call this function.

    0 讨论(0)
提交回复
热议问题