From 8dd33c6ba5e3a2f5461e9c7189b0683bc149634d Mon Sep 17 00:00:00 2001 From: James O'donnell Date: Wed, 31 Jan 2018 14:09:55 +0000 Subject: [PATCH 1/3] test posting invalid values --- flaskapp/tests.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/flaskapp/tests.py b/flaskapp/tests.py index 7d9dc70..ad4b358 100755 --- a/flaskapp/tests.py +++ b/flaskapp/tests.py @@ -28,7 +28,9 @@ def create_app(self): def test_runAll(self): self.server_is_up_and_running_test() - self.post_vals_works_test() + self.post_vals_works_with_correct_vals_test() + self.post_vals_fails_with_invalid_turnangle_test() + self.post_vals_fails_with_invalid_onoff_test() print 'all tests have run, shutting down' r = requests.post(url = self.url + 'shutdown', data = {}) @@ -39,7 +41,7 @@ def server_is_up_and_running_test(self): self.assertEqual(response.code, 200) print 'Server is up and running' - def post_vals_works_test(self): + def post_vals_works_with_correct_vals_test(self): data = {'onOff':1, 'turnAngle':41.0} urlPost = self.url + 'post' r = requests.post(url = urlPost, data = data) @@ -48,6 +50,20 @@ def post_vals_works_test(self): self.assertEqual(data['onOff'], retData['onOff']) print 'Successfully returned: '+r.text + def post_vals_fails_with_invalid_turnangle_test(self): + data = {'onOff':1, 'turnAngle':181.0} + urlPost = self.url + 'post' + r = requests.post(url = urlPost, data = data) + self.assertEqual('invalid request', r.text) + print 'Failed as expected: '+r.text + + def post_vals_fails_with_invalid_onoff_test(self): + data = {'onOff':2, 'turnAngle':41.0} + urlPost = self.url + 'post' + r = requests.post(url = urlPost, data = data) + self.assertEqual('invalid request', r.text) + print 'Failed as expected: '+r.text + if __name__ == '__main__': server = Process(target=flaskapp.main) From 84cebf8ee3ad51cc88d81d4da62661fb3f2a70c2 Mon Sep 17 00:00:00 2001 From: James O'donnell Date: Wed, 31 Jan 2018 14:43:38 +0000 Subject: [PATCH 2/3] restructured tests and test error handler --- flaskapp/tests.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/flaskapp/tests.py b/flaskapp/tests.py index ad4b358..ab5761b 100755 --- a/flaskapp/tests.py +++ b/flaskapp/tests.py @@ -26,22 +26,17 @@ def create_app(self): app.config['LIVESERVER_TIMEOUT'] = 10 return app - def test_runAll(self): - self.server_is_up_and_running_test() - self.post_vals_works_with_correct_vals_test() - self.post_vals_fails_with_invalid_turnangle_test() - self.post_vals_fails_with_invalid_onoff_test() - print 'all tests have run, shutting down' - r = requests.post(url = self.url + 'shutdown', data = {}) - - #Test that the server is responding - def server_is_up_and_running_test(self): + def test_server_is_up_and_running(self): print self.get_server_url() response = urllib2.urlopen(self.url) self.assertEqual(response.code, 200) print 'Server is up and running' - def post_vals_works_with_correct_vals_test(self): + #This must be part of the first test + print 'all tests have run, shutting down' + r = requests.post(url = self.url + 'shutdown', data = {}) + + def test_post_vals_works_with_correct_vals(self): data = {'onOff':1, 'turnAngle':41.0} urlPost = self.url + 'post' r = requests.post(url = urlPost, data = data) @@ -50,20 +45,24 @@ def post_vals_works_with_correct_vals_test(self): self.assertEqual(data['onOff'], retData['onOff']) print 'Successfully returned: '+r.text - def post_vals_fails_with_invalid_turnangle_test(self): + def test_post_vals_fails_with_invalid_turnangle(self): data = {'onOff':1, 'turnAngle':181.0} urlPost = self.url + 'post' r = requests.post(url = urlPost, data = data) self.assertEqual('invalid request', r.text) print 'Failed as expected: '+r.text - def post_vals_fails_with_invalid_onoff_test(self): + def test_post_vals_fails_with_invalid_onoff(self): data = {'onOff':2, 'turnAngle':41.0} urlPost = self.url + 'post' r = requests.post(url = urlPost, data = data) self.assertEqual('invalid request', r.text) print 'Failed as expected: '+r.text + def test_exception_handler(self): + ret = flaskapp.exception_handler("error") + self.assertEqual(ret, "Oh no! 'error'") + if __name__ == '__main__': server = Process(target=flaskapp.main) From c0259b3d792694faf476da9e5a8b343b923ba404 Mon Sep 17 00:00:00 2001 From: James O'donnell Date: Wed, 31 Jan 2018 15:18:03 +0000 Subject: [PATCH 3/3] test shutdown --- flaskapp/tests.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/flaskapp/tests.py b/flaskapp/tests.py index ab5761b..30b2400 100755 --- a/flaskapp/tests.py +++ b/flaskapp/tests.py @@ -26,16 +26,25 @@ def create_app(self): app.config['LIVESERVER_TIMEOUT'] = 10 return app + def setUp(self): + print 'setup' + + ###This must be first + def test_shutdown_text_outputted(self): + r = requests.post(url = self.url + 'shutdown', data = {}) + self.assertEqual(r.text, 'Server shutting down...') + + def test_shutdown_error_outputted(self): + with self.assertRaises(RuntimeError): + r = flaskapp.shutdown() + print 'Errored as expected: '+r.text + def test_server_is_up_and_running(self): print self.get_server_url() response = urllib2.urlopen(self.url) self.assertEqual(response.code, 200) print 'Server is up and running' - #This must be part of the first test - print 'all tests have run, shutting down' - r = requests.post(url = self.url + 'shutdown', data = {}) - def test_post_vals_works_with_correct_vals(self): data = {'onOff':1, 'turnAngle':41.0} urlPost = self.url + 'post' @@ -63,6 +72,9 @@ def test_exception_handler(self): ret = flaskapp.exception_handler("error") self.assertEqual(ret, "Oh no! 'error'") + def tearDown(self): + print 'teardown' + if __name__ == '__main__': server = Process(target=flaskapp.main)