Flask blueprint unit-testing
Asked Answered
A

3

18

Is there a good practice to unit-test a flask blueprint?

http://flask.pocoo.org/docs/testing/

I didn't found something that helped me or that is simple enough.

// Edit
Here are my code:

# -*- coding: utf-8 -*-
import sys
import os
import unittest
import flask

sys.path = [os.path.abspath('')] + sys.path

from app import create_app
from views import bp


class SimplepagesTestCase(unittest.TestCase):
    def setUp(self):
        self.app = create_app('development.py')
        self.test_client = self.app.test_client()

    def tearDown(self):
        pass

    def test_show(self):
        page = self.test_client.get('/')
        assert '404 Not Found' not in page.data


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

In this case, i test the blueprint. Not the entire app. To test the blueprint i've added the root path of the app to sys.path. Now i can import the create_app function to ...create the app. I also init the test_client.

I think i've found a good solution. Or will is there a better way?

Ancestor answered 13/11, 2013 at 19:19 Comment(0)
W
13

Blueprints are very similar to application. I guess that you want test test_client requests.

If you want test blueprint as part of your application then look like no differences there are with application.

If you want test blueprint as extension then you can create test application with own blueprint and test it.

Whelp answered 13/11, 2013 at 22:48 Comment(1)
Thanks, i think i've got it. I will post/edit the result soon.Ancestor
M
21

I did the following if this helps anyone. I basically made the test file my Flask application

from flask import Flask
import unittest

app = Flask(__name__)

from blueprint_file import blueprint
app.register_blueprint(blueprint, url_prefix='')

class BluePrintTestCase(unittest.TestCase):

    def setUp(self):
        self.app = app.test_client()

    def test_health(self):
        rv = self.app.get('/blueprint_path')
        print rv.data


if __name__ == '__main__':
    unittest.main()
Meyer answered 16/9, 2015 at 21:1 Comment(0)
W
13

Blueprints are very similar to application. I guess that you want test test_client requests.

If you want test blueprint as part of your application then look like no differences there are with application.

If you want test blueprint as extension then you can create test application with own blueprint and test it.

Whelp answered 13/11, 2013 at 22:48 Comment(1)
Thanks, i think i've got it. I will post/edit the result soon.Ancestor
M
0

I have multiple APIs in one app and therefore multiple blueprints with url_prefix. I did not like that I have to prefix all paths when testing on of the APIs. I used the following class to wrap the test_client for blueprints:

class BlueprintClient():
    def __init__(self, app_client, blueprint_url_prefix):
        self.app_client = app_client
        self.blueprint_url_prefix = blueprint_url_prefix.strip('/')

    def _delegate(self, method, path, *args, **kwargs):
        app_client_function = getattr(self.app_client, method)
        prefixed_path = '/%s/%s' % (self.blueprint_url_prefix, path.lstrip('/'))
        return app_client_function(prefixed_path, *args, **kwargs)

    def get(self, *args, **kwargs):
        return self._delegate('get', *args, **kwargs)

    def post(self, *args, **kwargs):
        return self._delegate('post', *args, **kwargs)

    def put(self, *args, **kwargs):
        return self._delegate('put', *args, **kwargs)

    def delete(self, *args, **kwargs):
        return self._delegate('delete', *args, **kwargs)

app_client = app.test_client()
api_client = BlueprintClient(app_client, '/api/v1')
api2_client = BlueprintClient(app_client, '/api/v2')
Murine answered 11/9, 2019 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.