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

后端 未结 8 1980
-上瘾入骨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: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();
        }
    

提交回复
热议问题