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
Provenance
- Commit
3053fcd- Tree
40c7e99- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
af3b73f
3 files changed+37 added−17 removed
modified:docs/testing.rst+23 added-13 removed
40 unmodified lines41 42 class FlaskrTestCase(unittest.TestCase):43 44 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()48 49+ def tearDown(self):50+ os.close(self.db_fd)51+ os.unlink(flaskr.DATABASE)52+53 if __name__ == '__main__':54 unittest.main()55 53-The code in the `setUp` function creates a new test client and initializes54-a new database. That function is called before each individual test function.55-What the test client does is give us a simple interface to the56-application. We can trigger test requests to the application and the57-client will also keep track of cookies for us.56+The code in the :meth:`~unittest.TestCase.setUp` method creates a new test57+client and initializes a new database. That function is called before58+each individual test function. To delete the database after the test, we59+close the file and remove it from the filesystem in the60+:meth:`~unittest.TestCase.tearDown` method. What the test client does is61+give us a simple interface to the application. We can trigger test62+requests to the application and the client will also keep track of cookies63+for us.64 65 Because SQLite3 is filesystem-based we can easily use the tempfile module60-to create a temporary database and initialize it. Just make sure that you61-keep a reference to the :class:`~tempfile.NamedTemporaryFile` around (we62-store it as `self.db` because of that) so that the garbage collector does63-not remove that object and with it the database from the filesystem.66+to create a temporary database and initialize it. The67+:func:`~tempfile.mkstemp` function does two things for us: it returns a68+low-level file handle and a random file name, the latter we use as69+database name. We just have to keep the `db_fd` around so that we can use70+the :func:`os.close` function to close the function.71 72 If we now run that testsuite, we should see the following output::73 74 $ python flaskr_tests.py17 unmodified lines92 93 class FlaskrTestCase(unittest.TestCase):94 95 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()99 100+ def tearDown(self):101+ os.close(self.db_fd)102+ os.unlink(flaskr.DATABASE)103+104 def test_empty_db(self):105 rv = self.app.get('/')106 assert 'No entries here so far' in rv.data107 89 unmodified linesmodified:examples/flaskr/flaskr_tests.py+7 added-2 removed
6 unmodified lines7 8 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11+import os12 import flaskr13 import unittest14 import tempfile15 1 unmodified line17 class FlaskrTestCase(unittest.TestCase):18 19 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()24 25+ def tearDown(self):26+ """Get rid of the database again after each test."""27+ os.close(self.db_fd)28+ os.unlink(flaskr.DATABASE)29+30 def login(self, username, password):31 return self.app.post('/login', data=dict(32 username=username,33 password=password37 unmodified linesmodified:examples/minitwit/minitwit_tests.py+7 added-2 removed
6 unmodified lines7 8 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11+import os12 import minitwit13 import unittest14 import tempfile15 1 unmodified line17 class MiniTwitTestCase(unittest.TestCase):18 19 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()24 25+ def tearDown(self):26+ """Get rid of the database again after each test."""27+ os.close(self.db_fd)28+ os.unlink(minitwit.DATABASE)29+30 # helper functions31 32 def register(self, username, password, password2=None, email=None):33 """Helper function to register a user"""116 unmodified lines