diff --git a/docs/testing.rst b/docs/testing.rst index 3ea1e9ba..eb4397a2 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -42,25 +42,32 @@ In order to test that, we add a second module ( class FlaskrTestCase(unittest.TestCase): def setUp(self): - self.db = tempfile.NamedTemporaryFile() + self.db_fd, flaskr.DATABASE = tempfile.mkstemp() self.app = flaskr.app.test_client() - flaskr.DATABASE = self.db.name flaskr.init_db() + def tearDown(self): + os.close(self.db_fd) + os.unlink(flaskr.DATABASE) + if __name__ == '__main__': unittest.main() -The code in the `setUp` function creates a new test client and initializes -a new database. That function is called before each individual test function. -What the test client does is give us a simple interface to the -application. We can trigger test requests to the application and the -client will also keep track of cookies for us. +The code in the :meth:`~unittest.TestCase.setUp` method creates a new test +client and initializes a new database. That function is called before +each individual test function. To delete the database after the test, we +close the file and remove it from the filesystem in the +:meth:`~unittest.TestCase.tearDown` method. What the test client does is +give us a simple interface to the application. We can trigger test +requests to the application and the client will also keep track of cookies +for us. Because SQLite3 is filesystem-based we can easily use the tempfile module -to create a temporary database and initialize it. Just make sure that you -keep a reference to the :class:`~tempfile.NamedTemporaryFile` around (we -store it as `self.db` because of that) so that the garbage collector does -not remove that object and with it the database from the filesystem. +to create a temporary database and initialize it. The +:func:`~tempfile.mkstemp` function does two things for us: it returns a +low-level file handle and a random file name, the latter we use as +database name. We just have to keep the `db_fd` around so that we can use +the :func:`os.close` function to close the function. If we now run that testsuite, we should see the following output:: @@ -86,11 +93,14 @@ this:: class FlaskrTestCase(unittest.TestCase): def setUp(self): - self.db = tempfile.NamedTemporaryFile() + self.db_fd, flaskr.DATABASE = tempfile.mkstemp() self.app = flaskr.app.test_client() - flaskr.DATABASE = self.db.name flaskr.init_db() + def tearDown(self): + os.close(self.db_fd) + os.unlink(flaskr.DATABASE) + def test_empty_db(self): rv = self.app.get('/') assert 'No entries here so far' in rv.data diff --git a/examples/flaskr/flaskr_tests.py b/examples/flaskr/flaskr_tests.py index 4355a650..9421cca6 100644 --- a/examples/flaskr/flaskr_tests.py +++ b/examples/flaskr/flaskr_tests.py @@ -8,6 +8,7 @@ :copyright: (c) 2010 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ +import os import flaskr import unittest import tempfile @@ -17,11 +18,15 @@ class FlaskrTestCase(unittest.TestCase): def setUp(self): """Before each test, set up a blank database""" - self.db = tempfile.NamedTemporaryFile() + self.db_fd, flaskr.DATABASE = tempfile.mkstemp() self.app = flaskr.app.test_client() - flaskr.DATABASE = self.db.name flaskr.init_db() + def tearDown(self): + """Get rid of the database again after each test.""" + os.close(self.db_fd) + os.unlink(flaskr.DATABASE) + def login(self, username, password): return self.app.post('/login', data=dict( username=username, diff --git a/examples/minitwit/minitwit_tests.py b/examples/minitwit/minitwit_tests.py index cd761aa6..10962142 100644 --- a/examples/minitwit/minitwit_tests.py +++ b/examples/minitwit/minitwit_tests.py @@ -8,6 +8,7 @@ :copyright: (c) 2010 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ +import os import minitwit import unittest import tempfile @@ -17,11 +18,15 @@ class MiniTwitTestCase(unittest.TestCase): def setUp(self): """Before each test, set up a blank database""" - self.db = tempfile.NamedTemporaryFile() + self.db_fd, minitwit.DATABASE = tempfile.mkstemp() self.app = minitwit.app.test_client() - minitwit.DATABASE = self.db.name minitwit.init_db() + def tearDown(self): + """Get rid of the database again after each test.""" + os.close(self.db_fd) + os.unlink(minitwit.DATABASE) + # helper functions def register(self, username, password, password2=None, email=None):