vendredi 19 février 2016

Android Integration Test of a Distributed Application (best practices)

I'm trying to write an integration test of my Android application. The application stores certain data at Google Cloud Datastore which are then being exchanged between different devices running the app.

And this is actually what I want to test - does the data exchange work fine? When one device saves piece of data to Datastore, does the other device(s) see(s)s it correctly, etc?

I was checking various Android testing tools but none of them seems to be supporting this testing use-case. Most of the time I was playing with Robolectric to shadow all the unnecessary part of the app and test only what's necessary but I've got to several troubles:

  • Robolectric seems not to be able to run multiple threads properly. As my app is sending data to Datastore in background threads, this is a main issue.
  • Robolectric seems to always run tests in a single JVM (let me know if I'm wrong here). As my app is using couple of static fields, it's not easy to simulate several devices and not to share this static fields.

Basically, I need to run the test on 2 (or more devices) at the same time when on each device different commands are executed but altogether they work seamlessly and correctly.

I've solved this by

  • bringing 'TestActivity' where I've implemented the test procedures for both of the devices; this activity UI has 2 buttons (Test A; Test B) which are both presented and by clicking on each of them at those 2 different devices, I'm able to run the test.
  • There was a small challenge with syncing those 2 devices but with a little effort I also solved this.
  • I also had to temporarily replace the main activity in AndroidManifest.xml with my TestActivity to present Test UI instead of the application UI.

enter image description here

My questions are as follows.

  1. Is this approach correct? None of the existing Test frameworks support running of tests at 2 or more "devices" simultaneously? It does not have to be real android devices (or emulators), JVM would be enough (like Robolectric is doing). Just I need to test the complex distributed logic at more JVMs at the same time. Or at least simulate such a run (there are static fields in my app.)
  2. How to keep such tests along with the main application? I had to implement couple of hacks into the app so the test has got access to internal structures (for test initialization and validation). I also had to implement the TestActivity and put it as the main activity. I don't want to check in these changes and build my main app as they will remain there. I have realized that an app (apk) cannot depend on another apk (the test application would depend on the main application and only would bring some additional functionality the test needs); apk can only depend on a library (aar). Does it mean my only option is to create an aar from my current apk and then bring 2 apks - one is the main one (which would probably contain nothing, just AndroidManifest.xml) and the other one would implement the test; both of the apks would be dependent on my aar?

Aucun commentaire:

Enregistrer un commentaire