Skip to content
Myr

Make the example tests pass on Windows.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Also updated the documentation regarding that.

authored by Armin Ronacher · committed by Armin Ronacher · parent af3b73f · Browse files · 3 files+37 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+37 added−17 removed

  • other:+37 added−17 removed

3 files+37 added−17 removed

modified:docs/testing.rst+23 added-13 removed
40 unmodified lines
4142 class FlaskrTestCase(unittest.TestCase):4344 def setUp(self):45 self.db = tempfile.NamedTemporaryFile()45 self.db_fd, flaskr.DATABASE = tempfile.mkstemp()46 self.app = flaskr.app.test_client()47 flaskr.DATABASE = self.db.name47 flaskr.init_db()4849 def tearDown(self):50 os.close(self.db_fd)51 os.unlink(flaskr.DATABASE)5253 if __name__ == '__main__':54 unittest.main()5553The code in the `setUp` function creates a new test client and initializes54a new database. That function is called before each individual test function.55What the test client does is give us a simple interface to the56application. We can trigger test requests to the application and the57client will also keep track of cookies for us.56The code in the :meth:`~unittest.TestCase.setUp` method creates a new test57client and initializes a new database. That function is called before58each individual test function. To delete the database after the test, we59close the file and remove it from the filesystem in the60:meth:`~unittest.TestCase.tearDown` method. What the test client does is61give us a simple interface to the application. We can trigger test62requests to the application and the client will also keep track of cookies63for us.6465Because SQLite3 is filesystem-based we can easily use the tempfile module60to create a temporary database and initialize it. Just make sure that you61keep a reference to the :class:`~tempfile.NamedTemporaryFile` around (we62store it as `self.db` because of that) so that the garbage collector does63not remove that object and with it the database from the filesystem.66to create a temporary database and initialize it. The67:func:`~tempfile.mkstemp` function does two things for us: it returns a68low-level file handle and a random file name, the latter we use as69database name. We just have to keep the `db_fd` around so that we can use70the :func:`os.close` function to close the function.7172If we now run that testsuite, we should see the following output::7374 $ python flaskr_tests.py
17 unmodified lines
9293 class FlaskrTestCase(unittest.TestCase):9495 def setUp(self):89 self.db = tempfile.NamedTemporaryFile()96 self.db_fd, flaskr.DATABASE = tempfile.mkstemp()97 self.app = flaskr.app.test_client()91 flaskr.DATABASE = self.db.name98 flaskr.init_db()99100 def tearDown(self):101 os.close(self.db_fd)102 os.unlink(flaskr.DATABASE)103104 def test_empty_db(self):105 rv = self.app.get('/')106 assert 'No entries here so far' in rv.data107
89 unmodified lines
modified:examples/flaskr/flaskr_tests.py+7 added-2 removed
6 unmodified lines
78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11import os12import flaskr13import unittest14import tempfile15
1 unmodified line
17class FlaskrTestCase(unittest.TestCase):1819 def setUp(self):20 """Before each test, set up a blank database"""20 self.db = tempfile.NamedTemporaryFile()21 self.db_fd, flaskr.DATABASE = tempfile.mkstemp()22 self.app = flaskr.app.test_client()22 flaskr.DATABASE = self.db.name23 flaskr.init_db()2425 def tearDown(self):26 """Get rid of the database again after each test."""27 os.close(self.db_fd)28 os.unlink(flaskr.DATABASE)2930 def login(self, username, password):31 return self.app.post('/login', data=dict(32 username=username,33 password=password
37 unmodified lines
modified:examples/minitwit/minitwit_tests.py+7 added-2 removed
6 unmodified lines
78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11import os12import minitwit13import unittest14import tempfile15
1 unmodified line
17class MiniTwitTestCase(unittest.TestCase):1819 def setUp(self):20 """Before each test, set up a blank database"""20 self.db = tempfile.NamedTemporaryFile()21 self.db_fd, minitwit.DATABASE = tempfile.mkstemp()22 self.app = minitwit.app.test_client()22 minitwit.DATABASE = self.db.name23 minitwit.init_db()2425 def tearDown(self):26 """Get rid of the database again after each test."""27 os.close(self.db_fd)28 os.unlink(minitwit.DATABASE)2930 # helper functions3132 def register(self, username, password, password2=None, email=None):33 """Helper function to register a user"""
116 unmodified lines