Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running unit tests on the "Flaskr" tutorial micro-blogging app in Flask

I've cloned the flaskr application from Github and am trying to follow the Testing Flask Applications tutorial. Following Bonus: Testing the Application, I've added a subdirectory test to the top-level flaskr directory, so that 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 directory tests; however, I'm writing tests in test_flaskr.py in the directory tests. So far I'm trying just 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 don't understand this error. My flaskr.py is the same as the one on https://github.com/pallets/flask/blob/master/examples/flaskr/flaskr/flaskr.py and has an init_db function defined. How can I make the unit test run?

like image 259
Kurt Peek Avatar asked Jan 12 '17 12:01

Kurt Peek


1 Answers

When you import the flaskr package in your script, you can access the variables, functions etc. declared and defined in flaskr/__init__.py

init_db is not defined in flask/__init__.py, the code provided, expects init_db to be defined in flask/__init__.py.

There are two ways to fix the issue you have:

  1. Replace flaskr.init_db() with flaskr.flaskr.init_db()

  2. Modify the import statement in test_flaskr.py as follows:

    Change import flaskr to from flaskr import flaskr

like image 170
metmirr Avatar answered Nov 14 '22 23:11

metmirr