lundi 9 juillet 2018

Flask application runs, but fails initial unit test

I just started unit testing my flask application and it fails to return a 200 status code when I test it, even though it works perfectly.

Here is the unit test code:

# dev-environment/test_basic.py

import unittest
from application import application, db

TEST_DB = "test.db"

class BasicTests(unittest.TestCase):
    def setUp(self):
        application.config['TESTING'] = True
        application.config['DEBUG'] = False
        application.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + TEST_DB
        self.application = application.test_client()
        db.drop_all()
        db.create_all()

        self.assertEqual(application.debug, False)

    def tearDown(self):
        pass

    def test_main_page(self):
        response = self.application.get("/welcome", follow_redirects=True)
        self.assertEqual(response.status_code, 200)


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

And here is my "/welcome" view in application.py:

# dev-environment/application.py

@application.route("/welcome")
def welcome():
    return render_template("welcome.html")

I get the following stack trace when I run the unit test file:

======================================================================
FAIL: test_main_page (__main__.BasicTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_basic.py", line 22, in test_main_page
    self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 1 test in 0.475s

FAILED (failures=1)

Aucun commentaire:

Enregistrer un commentaire