I think Flask wants me to instantiate the app in my tests.py file but i dont know how to

Viewed 1257

I think Flask wants me to instantiate the app but i dont know how to, am getting the error AttributeError: 'NoneType' object has no attribute 'app'

Traceback:

C:\Users\Mlamba\Envs\vir\Scripts\python.exe D:/code/web-projects/Bucketlist-Python-Flask-project/tests.py
E
======================================================================
ERROR: test_index_view (__main__.ViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:/code/web-projects/Bucketlist-Python-Flask-project/tests.py", line 11, in test_index_view
    response = make_response(render_template("index.html"))
  File "C:\Users\Mlamba\Envs\vir\lib\site-packages\flask\templating.py", line 132, in render_template
    ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

Run.py file:

from app import app

if __name__ == '__main__':
    app.run()

init.py file:

from flask import Flask

# Load the views
from app import views

# Initialize the app
app = Flask(__name__, instance_relative_config=True)

# Load the config file
app.config.from_object('config')

Test file:

import unittest

from flask import render_template, make_response


class ViewTests(unittest.TestCase):
    def test_index_view(self):
        """
        Test that index page is accessible without login
        """
        response = make_response(render_template("index.html"))
        self.assertEquals(response.status_code, 200)


if __name__ == '__main__':
    unittest.main()

Directory structure:

|-- README.md
|-- __pycache__
|   `-- config.cpython-36.pyc
|-- app
|   |-- __init__.py
|   |-- __pycache__
|   |   |-- __init__.cpython-36.pyc
|   |   `-- views.cpython-36.pyc
|   |-- models.py
|   |-- static
|   |-- templates
|   |   |-- index.html
|   |   `-- layout.html
|   `-- views.py
|-- config.py
|-- requirements.txt
|-- run.py
`-- tests.py
2 Answers
Related