Created
August 24, 2018 15:45
-
-
Save HorlogeSkynet/86eb37eb7fff8cf87700b75768d0d558 to your computer and use it in GitHub Desktop.
Flask v1.X testing example with UNITTEST Python module only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Flask v1.x testing class working with UNITTEST Python module.""" | |
# Run this module with : | |
# `python3 -m unittest test_flask_v1.py` | |
import os | |
import tempfile | |
import unittest | |
# Import your production module using Flask around here. | |
# The default `Flask` application module has been set for demo purposes only. | |
from flask import Flask | |
class TestFlaskV1(unittest.TestCase): | |
"""Flask testing class.""" | |
def setUp(self): | |
self.app = Flask(__name__) | |
self.db_fd, self.app.config['DATABASE'] = tempfile.mkstemp() | |
self.app.config['TESTING'] = True | |
self.app.config['SECRET_KEY'] = b'\xde\xad\xbe\xef' | |
@self.app.route('/a_web_path', methods=['GET', 'POST']) | |
def a_web_path(): | |
"""Simple test endpoint.""" | |
return 'It works.' | |
self.client = self.app.test_client() | |
def tearDown(self): | |
os.close(self.db_fd) | |
os.unlink(self.app.config['DATABASE']) | |
def test_your_testing_method(self): | |
"""Simple test method.""" | |
res = self.client.get('/a_web_path') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.data, b'It works.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment