Friday, November 4, 2011

Android code coverage

Unit tests are definitely a programmer's good friend (when done properly), watching the back and confirming that everything is working alright.

Code coverage is a friend of the unit tests, telling unit tests how well he has covered up his friend.

Anyways, I will give a quick and short instruction here about android unit testing.

Guess what? This is really simple if you have ever written any unit tests before for Java apps. It's all the same

  • First step, create a separate project for unit testing. When you create this project in Eclipse, eclipse will ask you to point to the existing project that you are going to test.
  • Write some unit tests. You will create test cases by extending AndroidTestCase class
  • Run the test, and see if the test method is actually being executed or not. Tests will be run in the emulator / device like a normal app, and will notify to eclipse. Use Eclipse > Show View > JUnit to see the result. There are notes about it in the same link I mentioned above.
  • Now getting the coverage report could not be any simpler:
    • create the ant script for building the test project. Open command prompt and go to your test project directory:
      • android update test-project -m <your_main_project_full_path> -p .
    • and run
      • ant coverage
Look for the coverage directory under test project and a nice report it waiting there.

Now if you are running in windows you may get this error
 "<something like File or SDK > was unexpected at this time.", 
then ant could not find some path. I hate windows "Programs Files" directory, cause that contains a space and space is an enemy for ant. So try Installing your sdk in a path that does not contain any space, and that should resolve it.

No comments:

Post a Comment