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

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

    Building on the answers above the following Kotlin code is equivalent:

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

    You can then check the value of the property:

    if (isRunningTest) {
      // Espresso only code
    }
    

提交回复
热议问题