Android automation using espresso without the app source code

后端 未结 4 877
醉梦人生
醉梦人生 2021-01-13 14:32

Is it not possible to automation android app using espresso without source code. Gradle expects a structure like this:

src/main/
src/androidTest/
         


        
4条回答
  •  孤城傲影
    2021-01-13 15:10

    The answer is yes, you can run automation test using Espresso without app source code.

    Espresso is based on Android instrumentation framework, which means the automation test is built into a single test apk. This test apk is different from normal application apk:

    1. There is an instrumentation registered in AndroidManifest.xml, which will be registered to Android system once test apk is installed

    2. The test apk must be signed using the same signature with the application apk, in order to run automation test

    3. The test apk runs in the same process as application apk

    Above are the only requirements of any instrument based test framework has. So there is no dependency of source code.

    But why we find most of the Espresso tutorials are mixed with source code? Because it will make the test simpler:

    1. You can easily control the activity lifecycle using class ActivityTestRule.

    2. You can test application defined classes easily.

    3. You can test UI widgets using widget id

    On the contrary, you have to write lots of reflection code to get the classes you need if you don't compile with source code. For example:

    1. You have to use Class.forName to load the entrance activity and launch it

    2. You have to use Java reflection to test application defined classes

    3. You have to use literal information to find UI widgets, because you don't have the id of the UI widgets

    To sum up, it is OK to run Espresso automation test without application source code, but it's much harder and make test codes ugly.

    You can refer the example project from AndroidTestWithoutSource.

提交回复
热议问题