I'm testing my application with Android Instrumentation tests.
So I have a test-class extending ActivityInstrumentationTestCase2
which contains multiple tests. The code looks like this:
public class ManageProjectsActivityTestextends ActivityInstrumentationTestCase2<ManageProjectsActivity> {
public ManageProjectsActivityTest() {
super("eu.vranckaert.worktime", ManageProjectsActivity.class);
}
@Override
protected void setUp() throws Exception {
getInstrumentation().getTargetContext().deleteDatabase(DaoConstants.DATABASE);
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
@Override
protected void runTest() throws Throwable {
super.runTest();
getActivity().finish();
}
public void testDefaults() {
// My test stuff
}
public void testAddProject() {
// My test stuff
}
}
So the activity which is under test has a list of projects. The list of projects is retrieved from the database. And when no database is available, so when the DB is created, I insert one default project.
So that means when the tests are run this is what I exepct:
- The database, if available, is removed on the device
- The first test is started (and thus the activity is launched which creates my DB with one project)
- The tests uses the newly created DB, so meaning with only one project, during the test a second project is created
- The first test is finished and the setUp() method is called again
- The database, that should exist now, is again removed
- The second test is started (and thus the activity is launched which creates my DB with one project)
- The second test also finishes
But that's not quite what this test-suite does... This is the result of my test-suite:
- The database, if available, is removed on the device
- The first test is started (and thus the activity is launched which creates my DB with one project)
- The tests uses the newly created DB, so meaning with only one project, during the test a second project is created
- The first test is finished and the setUp() method is called again
- The database, that should exist now, is again removed
- And here it comes: The second test is started (but my DB is not created again!!! I cannot see a file on the device either...) and the test should display only one project at the beginning but it does display already two!!!
- The second test also finishes but fails because I have two projects at the beginning...
At the beginning I did not override the runTest() method but I thought that maybe I should end the activity myself to force the re-creation, but it doesn't make any difference.
So it seems that the DB is kept in memory (as even no new DB file is created on the device when I explicitly remove it). Or even the activity, because when putting a breakpoint a in the onCreate of the activity I only get in there once for both tests.
For the maintaining the DB I use ORMLite. You can see my helper class here: http://code.google.com/p/worktime/source/browse/trunk/android-app/src/eu/vranckaert/worktime/dao/utils/DatabaseHelper.java
So my question is how to force the tests to use a different DB all the time...?