Matthew Slight
Matthew Slight

Reputation: 41

Testing Flask with Flask-Testing

I'm trying to write my first test for a simple Flask app. I simply want a test that will check if the app is running and gets a 200 response code by querying the root app url '/'.

When I run the test I get the following error:

raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: NOT FOUND

This suggests that the server is running but that the base url can't be found? What's going on?

import json
from urllib.request import urlopen
from flask import Flask
from flask_testing import LiveServerTestCase

class MyTest(LiveServerTestCase):

  def create_app(self):
    app = Flask(__name__)
    app.config['TESTING'] = True
    return app

  def test_flask_application_is_up_and_running(self):
    response = urlopen(self.get_server_url())
    self.assertEqual(response.code, 200)

Upvotes: 0

Views: 1058

Answers (1)

Nils Werner
Nils Werner

Reputation: 36765

I recommend reading the Flask testing docs: You usally don't go through network and HTTP to test your apps. Instead you create a test client from flask, which you then use to do requests directly to your app.

Upvotes: 1

Related Questions