Android App Code Coverage with Manual + Appium automation tests
Hello Everyone, In this article, I will explain how to get the Android Native app code coverage test reports with irrespective of the test style that you choose on real non rooted devices. With this approach, we will come to know that, how our test cases are dealing with the app and what are code areas it is getting touched or missed. Based on the reports, we can optimize/update the test cases to avoid product bugs. Here, I will explain this article by using a sample application so that it can reach a wider audience.
Software stack:
- Android app code and generate the instrument enabled app. (Apk file).
- Gradle to integrate android code with jacoco instrumentation.
- Adb to execute the shell commands.
Step 1: Integrate Jacoco instrumentation into Android App code:
- Add the Jacoco classpath dependencies plugin in the main application .gradle file.
- In the application module level gradle file add the jacoco-agent.prop location along with jacoco plugin details. Also, add jacoco test report generation task in the same gradle file. This is the task which we used to generate the test report.
- Make sure the jacoco + gradle plugin version are compatible otherwise it will give lot of issues.
Step 2: Creating an instrument activity and update the new activity details in manifest file.
- First figure out the entry point of the android application (Like splash screen or launcher activity). In our code it is AddNumber.java is the main entry point of the app.
- Create an InstrumentActivity listener interface, Instrument Activity, and JacocoInstrumentation class. Refer sample app code for more details.
- JacocoInstrumentation activity is our main activity which decides where to generate the jacoco test report files on activity destroy. Add this activity details into the manifest file. This is an activity we will launch to start the manual or automation test cases. Internally this will launch the Main activity of the app. Refer to the code for more details. This requires a little bit of knowledge on the Android App Activity life cycle.
- JacocoInstrumentation class is responsible for generating the code coverage files and it will keep the code coverage file in /sdcard/Download/coverage.ec path of the device. Always it will append to the same file.
- Once we perform all the test cases we can destroy (force close/kill) the app or we can trigger a broadcast event to generate the code coverage files (it is up to the requirement when do we want to generate the code coverage files). So JacocoInstrumentation class is responsible to generate these files.
- Once all the code setup is done, build the debug mode application (Because it has testcoverage enabled flag). Once the apk generated install the app to device by using the below command.
- adb install -g <<filename.apk>> this command will install the app with sdcard write permissions as well. If we don't give write permissions it will not generate the code coverage file.
- Once app is installed on the device, run the below command to start the app with instrumentation. We are launching the activity on the device. We can use the same command to launch the activity in automation as well.
adb shell am instrument -e coverage true -w suram.sridhar/suram.sridhar.JacocoInstrumentation.
9. Once the app is launched on the device, perform the manual test cases or run the automated test cases. Once the test are finished, kill the app so that it will generate the code coverage files in device /sdcard/Download/coverage.ec file.
10. Pull the file from device adb pull /sdcard/Download/coverage.ec to your local system.
11. Have a look at the jacoco test report task in the Gradle file which we created. Place the coverage.ec file accordingly. So that it will locate the file to run the task and it will generate the code coverage test report as below.
Refer below the video for more details about it.
//TODO - Will add in-detail video in couple of days.
References:
Sample android app: https://github.com/sridharreddysuram123/AndroidCodeCoverage
Appium UI Test code: https://github.com/sridharreddysuram123/appiumUItestWithCodeCoverage
Appium reference: https://appium.io/docs/en/writing-running-appium/android/android-coverage/#3-now-build-apk-and-run-appium-tests
Thank you for reading the article. Please feel free to post your questions/comments!
Consultant at capSpire | Software Developer | Rice ‘24
1 年Hi, was the video mentioned in this post ever made? Can it be found anywhere?