Monday, June 22, 2009

Android Setting Up A Testing Environment In Eclipse

As a new Android developer I have been pretty pleased with the quality of developer documentation. However, one area that still needs work is the documentation on testing. The Android developers’ site does provide documentation for the classes used in testing, but it is really just JavaDocs of methods, and is lacking a clear description of how to set up an environment to use them. Through trial and error and some Google searches, most notably a post by diego torres, I have learned to test my Android applications in Eclipse, and this post is to share that knowledge.

The first thing to understand is that you will need to have a separate android project for testing. When I first learned this I was disappointed, because I like to keep everything in one directory and I assumed that different projects meant different directories. However, the recommended set up is to have a “tests” folder in your main project’s root folder, and then use this new test folder as the root for a new android project you will use to run your tests. So you will have two android projects all within one main directory structure.

The steps go like this:

1. 1. Add a folder to the root of the android project you are going to be testing and name this new folder “tests”.

In Eclipse select File->New->Folder. In the dialog that appears enter or select the android project you are going to be testing, and enter tests as the folder name, then hit finish.

2. 2. The next step is to create a new android project to hold and run your tests. It should have its location set to the folder you created in the previous step.

In Eclipse select File->New->Android Project. In the dialog that appears give your testing project a name which can be whatever you like. In the contents section make sure you uncheck the check box titled “Use Default Location” and then set the location field to the tests folder you created in the previous step. In the Build Target section make sure you select the same target as you use in the project you are trying to test. Finally name this application which is again up to you, and specify a package name which should be of the format, a package in your original project that you want to test classes from .tests (ex project.base.tests or myapplication.tests). Uncheck the box next to “Create Activity:”, hit finish and your project should be created.



If you get an error that looks like this:

[2009-06-10 12:56:16 - TestProjectTest] no classfiles specified

[2009-06-10 12:56:16 - TestProjectTest] Conversion to Dalvik format failed with error 1

Don’t despair we will fix this in the next steps. Also a dialog may appear with an error about refreshing the workspace disregard this as well.

3. 3. The first thing we will do is set up your test project so that it can use the classes in your original project by adding the original project to the build path of your new project for testing.

In Eclipse right click on the very top folder that has your testing project name associated with it and select properties from the pop up list that appears. Once the properties dialog appears select “Java Build Path” from the list to the left. In the right of the dialog a tabbed interface should show up, select the “Projects” tab. Next click the add button inside the “Projects” tab. In the dialog that appears check the box next to the original project you are trying to test and hit ok. Then hit ok on the properties dialog and your testing project can now access the classes from your original project.

4. 4. Next you need to make some modifications to your new testing project’s manifest file (AndroidManifest.xml). Eclipse will set up your manifest initially to look something like this:

Subtle differences, especially involving names is fine. You need to edit your manifest to look like this:

The key things that need to be added have been bolded and marked in red. Make sure that the tag goes in between the and tags. And also make sure the tag does not go in between the application tags. The only things you need to change to fit your project have been marked in green. The first should be the root package of the original project you are trying to test, and the second is just a label name which can be whatever you want. After you make those changes your manifest should be good to go.

5. 5. At this point your test project is ready to go. Now you just need to start adding test classes. An example instrumental test class is below:

package project.base.tests;

import android.test.ActivityInstrumentationTestCase2;

import project.base.TestView;;

public class TestView_Functional_Test extends ActivityInstrumentationTestCase2 {

public TestView_Functional_Test() {

super("project.base", TestView.class);

}

@Override

protected void setUp() throws Exception {

super.setUp();

}

@Override

protected void tearDown() throws Exception {

super.tearDown();

}

public void testSomething() {

assertEquals("This will always be true an will never show this text.", false, true);

}

}

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Multiple projects are great, but they can also cause the loader to fail as described in http://jimshowalter.blogspot.com/2009/10/developing-android-with-multiple.html, which also explains how to fix the problem.

    ReplyDelete
  3. Thats is wierd. On android website it does not say anything about creating new folder in root directory. Is this you individual way of creating testing package ?

    ReplyDelete
  4. You no longer need to do this with the latest downloads, On the 3rd setup step of a new Android Project tick the box "Create Test Project" and it will setup everything exactly as described in the tutorial above. Even the manifest in the test project is correctly configured so you need do nothing more than put a tick in that box.

    ReplyDelete
  5. SEGA GENESIS - GAN-GAMING
    SEGA GENESIS. GENESIS-HANDS. Genesis https://tricktactoe.com/ (JP-EU). NA. https://vannienailor4166blog.blogspot.com/ NA. NA. SEGA GENESIS-HANDS. https://sol.edu.kg/ NA. https://deccasino.com/review/merit-casino/ SEGA GENESIS. NA. GENESIS-HANDS. NA. https://jancasino.com/review/merit-casino/

    ReplyDelete