The application of choice is obviously Junit, but, you need a few tweaks as you will want to run it on the device and you will also most probably need to obtain resources and contexts.
The first thing to do is make sure that you create a Test project when you create your main application base. This will put all of the test cases that you write into it.
To create a test case, just right click on the class you want to test and create a JUnit test case.
Once the test case is created, you will want access to the resources in the project. Change the test case so that it does not inherit from the regular JUnit test case, instead, use this:
android.test.AndroidTestCase
Now you will be able to write code such as this in the test case:
Resources resources = getContext().getResources();
InputStream inputStream = resources.openRawResource(com.myapp.R.raw.infile);
There is something important to realize here. The context that you obtain is from the application - not the test case. I can't find any way to put test files in the test case project and then access them. There probably is a way, but for now, I am not sure. Instead, if I want to use test data, I will just put test files in the application folder until I find a better way.
To run the test, just right click and do run as android junit test.
One final point. Sometimes the tests don't run the first time you invoke them like this. To fix this, just run the tests again and it seems to pick it up.
No comments:
Post a Comment