Created
July 10, 2012 08:16
-
-
Save z4r/3081985 to your computer and use it in GitHub Desktop.
Testing clients with Flask
This file contains hidden or 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
| from multiprocessing import Process | |
| from flask import Flask | |
| import requests | |
| import unittest2 as unittest | |
| app = Flask(__name__) | |
| @app.route('/hello/') | |
| @app.route('/hello/<user>') | |
| def hello(user='guest'): | |
| return "Hi there, %s!!!" % user | |
| @app.errorhandler(404) | |
| def page_not_found(e): | |
| return "Not Found", 404 | |
| class TestCaseWithFlask(unittest.TestCase): | |
| @classmethod | |
| def setUpClass(self): | |
| self.server = Process(target=self.application.run) | |
| self.server.start() | |
| @classmethod | |
| def tearDownClass(self): | |
| self.server.terminate() | |
| self.server.join() | |
| class TestCaseHello(TestCaseWithFlask): | |
| application = app | |
| def test_hello(self): | |
| r = requests.get('http://localhost:5000/hello') | |
| self.assertEqual(r.text, "Hi there, guest!!!") | |
| self.assertEqual(r.status_code, 200) | |
| def test_hello_name(self): | |
| r = requests.get('http://localhost:5000/hello/z4r') | |
| self.assertEqual(r.text, "Hi there, z4r!!!") | |
| self.assertEqual(r.status_code, 200) | |
| def test_hello_404(self): | |
| r = requests.get('http://localhost:5000/error') | |
| self.assertEqual(r.text, "Not Found") | |
| self.assertEqual(r.status_code, 404) | |
| if __name__ == '__main__': | |
| unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment