Search code examples
javaandroideclipseunit-testingjunit3

Sharing Android tests between two projects


I'm writing automated unit tests for an Android application, and I have come across a very odd situation.

The application is carefully constructed to target API8 - API 19 without needing multiple releases. One part of the tests, however, requires that I test a specific class without any internet access or mobile data. I've found a very hacky way to do this, but the solution comes in two parts: one for API 10 and above, and one for API 8 that requires a permission not available to applications targeting >= API 10.

What I want to do is to create two test projects, one that targets API 8 and one that targets everything else. What I want to avoid is duplicating the test files that work across all versions. As far as I can tell, there's only the manifest and a Utils class that needs to be different.

Is there any way to store the tests in a central location, and have them included and ran from both tests?


Solution

  • On Eclipse, create the tests to be shared on one of the projects, as usual.

    When you are happy with the test result, go to the other folder and create a new Folder inside:

    • Right Button on Project >> New >> Folder
    • Press "Advanced Button"
    • Select "Link to alternate location (Linked Folder)
    • Press the "Browse..." button and browse to the folder where previous tests are located.

    Finally, right click on the linked folder and select:

    • Build Path >> Use as Source folder.