Make the example tests pass on Windows.

Also updated the documentation regarding that.
This commit is contained in:
Armin Ronacher 2010-04-20 20:01:00 +02:00
parent af3b73f70d
commit 3053fcdb0d
3 changed files with 37 additions and 17 deletions

View file

@ -42,25 +42,32 @@ In order to test that, we add a second module (
class FlaskrTestCase(unittest.TestCase): class FlaskrTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.db = tempfile.NamedTemporaryFile() self.db_fd, flaskr.DATABASE = tempfile.mkstemp()
self.app = flaskr.app.test_client() self.app = flaskr.app.test_client()
flaskr.DATABASE = self.db.name
flaskr.init_db() flaskr.init_db()
def tearDown(self):
os.close(self.db_fd)
os.unlink(flaskr.DATABASE)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
The code in the `setUp` function creates a new test client and initializes The code in the :meth:`~unittest.TestCase.setUp` method creates a new test
a new database. That function is called before each individual test function. client and initializes a new database. That function is called before
What the test client does is give us a simple interface to the each individual test function. To delete the database after the test, we
application. We can trigger test requests to the application and the close the file and remove it from the filesystem in the
client will also keep track of cookies for us. :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 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 to create a temporary database and initialize it. The
keep a reference to the :class:`~tempfile.NamedTemporaryFile` around (we :func:`~tempfile.mkstemp` function does two things for us: it returns a
store it as `self.db` because of that) so that the garbage collector does low-level file handle and a random file name, the latter we use as
not remove that object and with it the database from the filesystem. 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:: If we now run that testsuite, we should see the following output::
@ -86,11 +93,14 @@ this::
class FlaskrTestCase(unittest.TestCase): class FlaskrTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.db = tempfile.NamedTemporaryFile() self.db_fd, flaskr.DATABASE = tempfile.mkstemp()
self.app = flaskr.app.test_client() self.app = flaskr.app.test_client()
flaskr.DATABASE = self.db.name
flaskr.init_db() flaskr.init_db()
def tearDown(self):
os.close(self.db_fd)
os.unlink(flaskr.DATABASE)
def test_empty_db(self): def test_empty_db(self):
rv = self.app.get('/') rv = self.app.get('/')
assert 'No entries here so far' in rv.data assert 'No entries here so far' in rv.data

View file

@ -8,6 +8,7 @@
:copyright: (c) 2010 by Armin Ronacher. :copyright: (c) 2010 by Armin Ronacher.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import os
import flaskr import flaskr
import unittest import unittest
import tempfile import tempfile
@ -17,11 +18,15 @@ class FlaskrTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
"""Before each test, set up a blank database""" """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() self.app = flaskr.app.test_client()
flaskr.DATABASE = self.db.name
flaskr.init_db() 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): def login(self, username, password):
return self.app.post('/login', data=dict( return self.app.post('/login', data=dict(
username=username, username=username,

View file

@ -8,6 +8,7 @@
:copyright: (c) 2010 by Armin Ronacher. :copyright: (c) 2010 by Armin Ronacher.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import os
import minitwit import minitwit
import unittest import unittest
import tempfile import tempfile
@ -17,11 +18,15 @@ class MiniTwitTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
"""Before each test, set up a blank database""" """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() self.app = minitwit.app.test_client()
minitwit.DATABASE = self.db.name
minitwit.init_db() 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 # helper functions
def register(self, username, password, password2=None, email=None): def register(self, username, password, password2=None, email=None):