27

How do I create an Android Test project in Eclipse in the target project's /test directory?

Note: I'm just doing my own Q+A for people according to the Stackoverflow FAQ on answering your own question.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Tim
  • 5,767
  • 9
  • 45
  • 60

2 Answers2

27

I am using Helios with the latest current version of Android (10.0.1)

The Android Testing fundamentals recommends you place the tests in the same project folder as your src folder. Some people have recommended that you create a new test project in its own folder so it doesn't get packaged into the apk. I looked inside my apk and I don't think the test stuff was included (but I could be wrong). I like having it in the same directory as its more convenient for revision control - you don't need to sync two projects each time.

Assume we want to create a test suite for the MyAndroidApp project

Goto File.. New.. Other.. Android...Android Test Project

  • Set "Test Project Name" to MyAndroidAppTest
  • For Test Target, choose "An existing Android project".. Use the Browse button to select MyAndroidApp
  • Unselect "Use default location". This changes the location to be inside the MyAndroidApp project under the tests directory
  • Click Finish

When it is being created, if you get a An internal error occurred during: "Refreshing workspace". error (or other errors) just ignore it. Click OK.

-Clean the MyAndroidApp project

Even though the project is in the <workspace path>/MyAndroidApp/tests directory it will appear as a normal Eclipse project in the package explorer. Clean the MyAndroidAppTest project. This worked for me.

EDIT

If you upload to SVN in Eclipse (by syncing the MyAndroidApp project, not the MyAndroidAppTest project) you need to do a little more (I've just done it now). So when you update your other working copy it will pull down the /tests directory. The MyAndroidAppTest project does not automatically show up in the Package Explorer. So you need to go to File.. Import...Existing Projects into Workspace.... Then select the root directory by browsing to your <workspace path>/MyAndroidApp/tests directory and click Finish.

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
Tim
  • 5,767
  • 9
  • 45
  • 60
  • Works fine. No SVN issues for me. I'm using Ubuntu 10.04, Helios, Subclipse and Unfuddle. – user77115 Aug 10 '11 at 17:36
  • 23
    When I do this, I get the test project in a subdirectory "MyAndroidAppTestTest" (yep with two Tests) rather than "tests". – Kurt Jun 28 '12 at 14:59
  • 10
    @Kurt if you rename your "MyAndroidAppTestTest" to "tests" and then create new project from existing source and then again the new created project to "MyAndroidAppTest" problem solved. – pharaoh Aug 24 '12 at 03:21
  • 1
    I have the same problem than @Kurt, is this a bug ?. – Sergio Feb 07 '13 at 20:21
  • I also have the same problem as @Kurt and Sergio, and I agree it seems like a bug. If you have this problem too, please star the bug report I have created here: https://code.google.com/p/android/issues/detail?id=54876 –  May 02 '13 at 01:38
  • Btw, @pharaoh, please could you explain exactly the steps you meant when you advised to "create new project from existing source"? Thanks in advance! –  May 02 '13 at 01:42
  • My project is for some reason not added properly and I get following error when trying to run (it can't run the correct project): Activity does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml – Warpzit Mar 19 '14 at 10:15
1

I found here a very detailed solution to this problem. It seems to be an ADT bug.

http://jonblack.org/2012/11/24/creating-an-android-test-project-within-a-project

Still, when I import the test project back into the workspace at Step 5 I get the following error message:

java.io.FileNotFoundException: C:\projects\MyApp\com.myapp.test-test\project.properties ( The system could not finde the file specified ).

I simply closed the error dialog and the test project appeared in my workspace at the same level as my android project, but the location was correctly set to MyApp/tests. So I wrote a test, run it and it worked.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
viktor0710
  • 131
  • 1
  • 6