I cloned a flaskr application from Github and try to follow Testing flask applications . After Bonus: testing the application , I added the test
subdirectory to the top-level flaskr
, so my directory tree looks like this:
. βββ build β βββ bdist.linux-x86_64 β βββ lib.linux-x86_64-2.7 β βββ flaskr β βββ flaskr.py β βββ __init__.py β βββ schema.sql β βββ static β β βββ style.css β βββ templates β βββ layout.html β βββ login.html β βββ show_entries.html βββ dist β βββ flaskr-0.0.0-py2.7.egg βββ flaskr β βββ flaskr.db β βββ flaskr.py β βββ flaskr.pyc β βββ __init__.py β βββ __init__.pyc β βββ schema.sql β βββ static β β βββ style.css β βββ templates β βββ layout.html β βββ login.html β βββ show_entries.html βββ flaskr.egg-info β βββ dependency_links.txt β βββ PKG-INFO β βββ requires.txt β βββ SOURCES.txt β βββ top_level.txt βββ MANIFEST.in βββ README βββ setup.cfg βββ setup.py βββ test β βββ test_flaskr.py βββ tests βββ test_flaskr.py
Note that there are also "built-in" tests in the tests
directory; however, I write tests in test_flaskr.py
in the tests
directory. So far I have tried only one test:
import os import flaskr import unittest import tempfile class FlaskrTestCase(unittest.TestCase): def setUp(self): self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp() flaskr.app.config['TESTING'] = True self.app = flaskr.app.test_client() with flaskr.app.app_context(): flaskr.init_db() def tearDown(self): os.close(self.db_fd) os.unlink(flaskr.app.config['DATABASE']) def test_empty_db(self): rv = self.app.get('/') assert b'No entries here so far' in rv.data if __name__ == '__main__': unittest.main()
However, if I try to run this, I get the following error:
Traceback (most recent call last): File "/home/kurt/dev/scratch/flask/examples/flaskr/test/test_flaskr.py", line 13, in setUp flaskr.init_db() AttributeError: 'module' object has no attribute 'init_db'
I do not understand this error. My flaskr.py
is the same as https://github.com/pallets/flask/blob/master/examples/flaskr/flaskr/flaskr.py and has an init_db
function. How can I run unit test?
source share