Android :: Unit Testing & Using A Different Database File
Mar 30, 2010
I'm trying to start unit testing on my application (should have done so from the beginning). I've got an Eclipse project structure set up and everything seems to work well, but...
I'm subclassing SQLiteOpenHelper to access the application database. This works well for the application, but when using the same class in the unit tests, it writes to the same database as the actual application. This is obviously rather irritating, since unit test data shows up when developing and testing the actual application.
What's the best way to make the SQLiteOpenHelper class write to a different database file when being called from the unit tests?
I was finally able to build and debug the android platform (the process can be seen here). Now, the question is: where are the unit tests, how do I launch them and where do I see results? And also, is there any guide to android platform architecture?
There is no way to load a string from a specific locale's language file.
What I want to do is run all my locales' (I have 24 of them) time format strings through unit tests to make sure they don't crash. For example, here's a date format string in French: "'Hier Ã ' h:mm a".
These really need to be unit tested, because translators aren't great about properly escaping these strings, causing crashes unless we manually verify every string in every language every time we get a translation pass back.
This question has a sort-of answer. Is that really the only way? Is there a better way to specify the locale of a Context in unit tests?
I my main app and tests are organized like this (standard from "android create project"): AndroidManifest.xml assets/ main app asset files src/tests/AndroidManifest.xml -- uses <instrumentation> to point to the main app assets/ -- test-specific asset files src/ Writing test cases with AndroidTestCase, I'd like to load asset files from the test-specific assets/ directory, not from the main app assets/ directory. How can I accomplish that? The normal way to load an asset would be getContext().getAssets().open ("foo.txt"), for assets/foo.txt in the main app. I assume I just have to somehow change the Context or the AssetManager to point to the test- specific assets directory, but I don't see any way to do that. I have looked into the tests/bin/MyApp-debug.apk to confirm that the test assets are in there. Now I just need a way to access them.
I want to start with a consistent test environment so I need to reset/clear my preferences. Here's the SetUp for test I have so far. It's not reporting any errors, and my tests pass, but the preferences are not being cleared.
I'm testing the "MainMenu" activity, but I temporarily switch to the OptionScreen activity (which extends Android's PreferenceActivity class.)
I do see the test correctly open the OptionScreen during the run.
I'm trying to run unit tests on my Android app and I'm getting a NoClassDefFoundError when referencing 3rd party jars.
The classes in those jars are only referenced by the app itself, not the unit tests. The jars are on the apps classpath and have been exported in the eclipse settings to allow tyhem to be found by dependant apps - which, of course, the test app is.
I need to run unit tests for code that references SQLiteDatabase for my Android code; however all my attempts to instantiate this object outside the emulator (on my desktop machine) have failed. JDBC on Android is not being recommended on the Net, hence it's out of the question (I could have provided mock objects very easily that way).
What is the easiest way to create some kind of test harness for Android apps and to be able to visualize (even on the emulator) what is going into the db? I want to test things like constraints, look at the data, unit test methods, etc. Is there any kind of GUI tool that I can hook up to the emulator to be able to look at the db of a particular app while I'm developing/testing, or are my only options the adb shell and creating test Activities within my app?
I want to test parsing of data returned from server.I thought I might create several test XML files and then feed them to the sax parser which uses my custom data handler (as in the application that I test).But where should I put those test XMLs?I tried with [project_root] /res/xml, but then I get the error: android.content.res.Resources $NotFoundException: Resource ID #0x7f040000 type #0x1c is not valid at android.content. res. Resources. loadXml ResourceParser(Resources.java:1870)at android.content.res. Resources. getXml(Resources. java:779) Does that mean that the xml is invalid, or that android couldn't find the XML file?(I use the same XML that comes from the server - copied it from the firebug's net panel and pasted into the file).Can I somehow read that XML as a text file, since etContext(). getResources() .getXml (R.xml.test_response1) returns XmlResourceParser instead of String (which I'd prefer)?
I'd love to get some ideas on best practices for testing our Android software, especially for AT&T devices. We're using a Samsung Captivate, on AT&T. As you know, AT&T disables sideloading (no Unknowns Sources option in Settings->Applications). We need to have our QA team load our apk file onto these devices as part of our testing and validation process. We're willing to install the SDK if needed, and upload the apk file via USB. We are not willing to root the phone.
I'm writing an Android JUnit test and want to copy/reset a test fixture file (it's an SQLite database file.) If I were within the main application, I know I could just place the file in the assets directory and use getResources().getAssets().open(sourceFile).However, this API appears to be unavailable from the ActivityInstrumentationTestCase2 class.Is there an easy way to copy a file over from the testing PC, or should I just keep a fresh copy of a test fixture on the device and copy it over a temporary file?
I need to somehow backup my applitcations database to a file so that it can later be retored using that file. The only way I would do it would be by reading every table to an xml ffile but it seems soo ineficient. The data does not have to be humand readable.
Is it possible to take a csv file stored in the res/raw resource directory and use it to populate a table in the sqlite3 database? My thought was that, if there was a way to do a bulk import for the entire file into the table then that would be cleaner and faster than iterating over each line in the file and executing individual insert statements...
but I'm having trouble applying those statements in my Android application. My first thought was to try something like the following...but no luck:
Using Local XML file as a database for a android app?
I am trying to build a app with flash using a xml file as database, to read the xml from a external file from the local directory isn't a problem, the problem is how to write data from flash to the xml file.
Is it possible to choose a custom location for the sqlite database file? If possible I want to store the database file in the memory card. And if user switches from one memory card to the other I want my application to use whatever version of the database file available on the card.
Im doing an app which uses sqlite db file in assets folder, There is a screen in my app having a button 'check for update'. Client has given me an URL link to update db file(which gives a sqlite file). When user clicks on the button i need to upgrade the old db file with new db file from URL.
My plan is to prepopulate a database on a virtual device and then include database in the distribution of my app.However, I can't find the database file.Is it on my hard disk some where?How do I get it?I tried connecting using adb, I did an "ls" and then got really scared by this obscure list of directories.