Just out of interest - why has it been decided to move tests to a separate project, not just to a separate source folder?
3 Answers
The test application "instruments" the target or main application.
The instrumentation section in the test project's AndroidManifest.xml allows the tests to run in the same process as the application. This instrumentation feature allows the test application to step through android component lifecycles in a controlled way.
Having this control allows you to (for example) create repeatable tests for cases for the activity lifecycle (create,resume,pause,destroy).
see http://developer.android.com/guide/topics/testing/testing_android.html#Instrumentation
So in summary, the extra application has special powers over the test target. Since these are encapsulated in the test application, your real application need only have the permissions it requires to perform its duty.

- 646
- 7
- 17
-
Not sure this is true, from my experience permissions utilised in the test must be declared in the test target manifest (which is pretty poor if you ask me) – rogermushroom Mar 29 '11 at 05:14
-
It is true that some test libraries require that the target has extra permissions. For example, I think Robotium used to require the permission to see which processes are running.I use robotium and agree that this wasn't ideal. However robotium no longer appears to require this. I assume that since the test app runs as part of the target application process it uses the target's permissions. Can you be more specific about your experience? My answer is to the question "why has it been decided to move tests to a separate project". – byeo Mar 29 '11 at 07:20
-
A simple example, I have App A and Test Project B which tests App A. If I want to use the internet in the Test Project B to verify a result (I know, it's a poor test) but App A doesn't have internet permissions I must add the to App A anyway therefore giving a production App unnecessary permission which is wrong. seen as the test project has it's own manifest it would be better to be able to define test specific permissions in there, although I understand on a low level this would be very difficult – rogermushroom Mar 29 '11 at 14:12
-
I agree it would be nice if the test could have extra permissions. By 'special powers' I meant instrumentation which allows the test app to control and step the normal (create/start/pause/destroy) lifecycle. – byeo May 26 '11 at 13:21
The current documentation, as I interpret it, states that you should hold the test code in a separate project in folder "tests" inside the applicaton project => "A project within a project"
http://developer.android.com/guide/topics/testing/testing_android.html#TestProjects
How ?
Creating an Android Test project in Eclipse
Why ?
Instead of ex. 40 projects you have 20 projects => better overview, less maintenance, faster eclipse.
Two build.xml => easier CI builds with Jenkins
Two manifest files => future versions of ADT build tools will support manifest merging
I think that this way you can do two separate apps, one that is the regular one, and one to test it. If you want to publish your app, you don't need to give to everyone your tests. But maybe it has be done for better purposes!

- 16,210
- 2
- 40
- 69

- 89
- 2
- 11
-
Yes this is done so that test code is not built into the application itself, allowing the test code to be run against the final production version of the app without having to increase the app size with all of the test code built into it. – hackbod Jan 16 '11 at 21:48
-
Oh note that you *can* build your tests directly into your app, there is nothing preventing that. There is just no good reason to do so, since the test code links to the app code so has full access to it. – hackbod Jan 16 '11 at 21:49
-
1But it is stated on the android dev site that "the best approach is to add the test project so that its root directory tests/ is at the same level as the src/ directory of the main application's project." meaning tests are built into the application. – Eugene Jan 16 '11 at 23:55