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

后端 未结 8 1928
-上瘾入骨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: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.

提交回复
热议问题