Search code examples
unit-testingsqlitenosetestsweb.py

How to use SQLite :memory: database in webpy for unittesting


I want to use a SQLite in memory (":memory:") DB for the tests in my webapp. I'm using nosetests for the tests, and webpy as framework.

I want to populate the DB in the setup() function, and then run all my tests. My problem is that webpy closes all the open DB connections after each request, and the SQLite :memory: DB only lasts until you close the connection, so only the first test is actually run correctly and all the others fail.

My choices are either to run the tests on a disk backed DB, or to recreate the entire DB in memory at the beginning of each individual test.

Do you know how can I prevent webpy from closing DB connections after each request? Can you think of any other way to get an in memory SQLite DB that lasts for more than one request using webpy?


Solution

  • Maybe you could run the tests on a DB stored on the disk, but using a RAM disk. In Windows, you can install a driver to set up a RAM disk (some instructions here). In Linux, I believe you want to set up tmpfs.

    A ram disk will act exactly like a hard disk, but will operate completely from memory, so that you will lose some of the overhead of loading files to/from the hard disk.