Skip to content

Commit

Permalink
Make the example tests pass on Windows.
Browse files Browse the repository at this point in the history
Also updated the documentation regarding that.
  • Loading branch information
mitsuhiko committed Apr 20, 2010
1 parent af3b73f commit 3053fcd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
36 changes: 23 additions & 13 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions examples/flaskr/flaskr_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions examples/minitwit/minitwit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 3053fcd

Please sign in to comment.