0

I am running unittests in a Flask app and I keep getting 404 when views.py file is not imported even though it is not used. I have such tests.py package:

import unittest from presence_analyzer import main, utils from presence_analyzer import views class PresenceAnalyzerViewsTestCase(unittest.TestCase): def setUp(self): self.client = main.app.test_client() def test_mainpage(self): resp = self.client.get('/') self.assertEqual(resp.status_code, 302) 

When I delete views import the described problem occurs. Views are organized in a similar way to this:

from presence_analyzer.main import app @app.route('/') def mainpage(): return redirect('/static/presence_weekday.html') 

And the main.py file:

import os.path from flask import Flask app = Flask(__name__) # pylint: disable=invalid-name app.config.update( DEBUG=True, ) 

I guess it's something similar to what happened in this case, so I'm trying to change the application so that I don't have to make this dumb imports while testing. I've been trying to make use of the answer from above, but still can't make it work and these docs don't seem helpful. What am I doing wrong? main.py:

from flask.blueprints import Blueprint PROJECT_NAME = 'presence_analyzer' blue_print = Blueprint(PROJECT_NAME, __name__) def create_app(): app_to_create = Flask(__name__) # pylint: disable=invalid-name app_to_create.register_blueprint(blue_print) return app_to_create app = create_app() 

views.py:

from presence_analyzer.main import app, blue_print @blue_print.route('/') def mainpage(): return redirect('/static/presence_weekday.html') 

And tests.py has remained unchanged.

0

1 Answer 1

1

You must import views, or the route will not be registered. No, you are not executing the views directly, but importing executes code all module-level code. Executing code calls route. route registers the view function. You cannot get around needing to import a module in order to use the module.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.