The best starting point is to have a look at the ApiDemos sample that comes with the SDK. It has a complete and working setup for a variety of tests. The first steps are to make sure that this actually works on your machine.
- First of all, build the officially supplied test suite. This does not work out of Eclipse as far as I can tell, so just go to a command line window and go to
/samples/ApiDemos/tests and do a build with ant.
- You might find that the ApiDemos itself was not built before, so go there first.
- Once the ApiDemos is built, try building the test again.
- Now you need to install the ApiDemos and the tests. This is done by going into the respective bin directories and run adb install ApiDemos-debug.apk and adb install ApiDemosTest-debug.apk. This requires that the emulator is running. Mine is, as I started it from Eclipse.
- Now restart the emulator. This is important as otherwise it might not know that these new apks have been installed. I made the mistake of letting it run, and I always got an error when running with the suggested commands.
- Run something like
$ adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner
The tests should now run (some fail for me, but the main machinery is up and running).
Now ist the time to get my own tests in:
- I recreate the directory structure for my tests from the Google template and copy the AndroidManifest.xml and build.xml. Both will need some modification to make sure that my classes will be tested.
- In the src directory I adjust the files for the top-level test runners and add a directory that will exercise my own TracklogViewer.
- The build with ant fails on a few points. -e option as usual gives a bit more information. Some directories need to be created by hand as they were expected but are not there.
- I install my own test apk on the emulator.
- I run the instrumentation.
- It fails. The reason, it transpires, is that I have not restarted the emulator.
- It finally runs. As the tests are pretty empty, there is no error.
Now it comes to actually implementing a few tests. But that is another story.