def app(request): app = create_app({'TESTING': True}) # Establish an application context before running the tests. ctx = app.app_context() ctx.push() def teardown(): ctx.pop() request.addfinalizer(teardown) return app
class TestShortlink: """ GIVEN a Flask application configured for testing WHEN the '/shortlinks' page is is posted to (POST) for bitly/tinyurl THEN check that a '200' status code is returned and also getting a valid JSON FALLING back to tinyurl if there is an unkonwn provider """ url = '/shortlinks' mimetype = 'application/json' headers = { 'Content-Type': mimetype, 'Accept': mimetype } flask_app = create_app({ 'TESTING': True }) def test_shortlink_bitly(self): """ GIVEN a Flask application configured for testing WHEN the '/shortlinks' page is is posted to (POST) with 'bitly' provider THEN check that a '200' status code is returned and also getting a valid JSON """ data = { 'url': 'http://google.com/test/123', 'provider': 'bitly' } with self.flask_app.test_client() as test_client: response = test_client.post(self.url, data=json.dumps(data), headers=self.headers) assert response.status_code == 200 assert response.json['url'] == data['url'] assert response.json['link'] != '' and 'https://bit.ly/' in response.json['link'] def test_shortlink_tinyurl(self): """ GIVEN a Flask application configured for testing WHEN the '/shortlinks' page is is posted to (POST) with 'tinyurl' provider THEN check that a '200' status code is returned and also getting a valid JSON """ data = { 'url': 'http://google.com/test/123', 'provider': 'tinyurl' } with self.flask_app.test_client() as test_client: response = test_client.post(self.url, data=json.dumps(data), headers=self.headers) assert response.status_code == 200 assert response.json['url'] == data['url'] assert response.json['link'] != '' and 'https://tinyurl.com/' in response.json['link'] def test_shortlink_default(self): """ GIVEN a Flask application configured for testing WHEN the '/shortlinks' page is is posted to (POST) with ???UKNOWN??? provider THEN use 'tinyurl' as the default provider and check that a '200' status code is returned and also getting a valid JSON """ data = { 'url': 'http://google.com/test/123', 'provider': 'other-provider' } with self.flask_app.test_client() as test_client: response = test_client.post(self.url, data=json.dumps(data), headers=self.headers) assert response.status_code == 200 assert response.json['url'] == data['url'] assert response.json['link'] != '' and 'https://tinyurl.com/' in response.json['link']
from shorty.app import create_app # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # create the application app = create_app() # run it if __name__ == '__main__': app.run(host='0.0.0.0', debug=True) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_config(): assert not create_app().testing assert create_app({'TESTING': True}).testing