Thursday, March 22, 2012

[android-developers] Re: Android instrumentation testing, how to start with clean database all the time

I did not really found the answer, but I did find a solution (or another way to reach my target :) ).
See my answer at StackOverflow.com =>  http://stackoverflow.com/questions/9804917/android-testing-start-with-clean-database-for-every-test/9823600#9823600 

Op woensdag 21 maart 2012 09:26:56 UTC+1 schreef Dirk Vranckaert het volgende:
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: http://pastebin.com/0Di3Hwug 

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.

What I want it to do is to delete the database before every test is run (that's why I have the delete in the setUp() method), and so  if there is no database available the onCreate for the database should be called on creation of my activity (when loading the list of projects).

What it does, it deletes the DB, it creates the DB, then runs my first test, then it deletes the DB, but does not create it, and runs the second test.

In the test that is first run, I create (add) a project to the DB. In the second test, as I delete the DB, I don't want to see that project anymore but it's there!! No idea how that can happen but it seems that the instrumentation tests are keeping a DB instance in memory...
Or even an instance of the activity? Because when I put a breakpoint in the onCreate of my activity I only get there once for both the 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 how can I work around this or is there another way to achieve what I want?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment