class RouteTestCase(BaseTestCase): """ Base test class for dealing with HTTP route handlers. """ def setUp(self): super(RouteTestCase, self).setUp() # Setup app for testing self.app = TestApp(app, db=db, use_session_scopes=True) self.app_context = app.app_context() self.app_context.push() # Setup database for testing self.db = db self.db.create_all() # Create a new user user = User( email='*****@*****.**', password='******') self.db.session.add(user) self.db.session.commit() # Assign the user to the session with self.app.session_transaction() as sess: sess['user_id'] = user.id def tearDown(self): # Clean up the test app and database db.drop_all() self.app_context.pop()
class TestMainFeatures(unittest.TestCase): def setUp(self): self.app = app1 self.w = TestApp(self.app) def test_single_template(self): r = self.w.get('/') self.assertFalse(r.flashes) self.assertEqual(len(r.contexts), 1) self.assertEqual(r.context['text'], 'Hello!') self.assertEqual(r.template, 'template.html') self.assertNotIn('qwerty', r.session) def test_two_templates_and_flash_messages(self): r = self.w.get('/').form.submit() self.assertEqual(len(r.contexts), 2) if flask_gte_0_10: self.assertEqual(len(r.flashes), 2) category, message = r.flashes[0] self.assertEqual(message, 'You have pressed "Quit"...') category, message = r.flashes[1] self.assertEqual(message, 'Flash message that will never be shown') else: self.assertEqual(len(r.flashes), 1) category, message = r.flashes[0] self.assertEqual(message, 'You have pressed "Quit"...') with self.assertRaises(AssertionError): r.context # Because there are more than one used templates self.assertEqual(r.contexts['template.html']['text'], 'Goodbye!') self.assertEqual(r.contexts['extra-template.html']['extra_text'], 'Some text.') def test_session_transaction(self): r = self.w.get('/whoami/') self.assertEqual(r.body.decode('utf-8'), 'nobody') with self.w.session_transaction() as sess: sess['username'] = '******' r = self.w.get('/whoami/') self.assertEqual(r.session['username'], 'aromanovich') self.assertEqual(r.body.decode('utf-8'), 'aromanovich') def test_init(self): w = TestApp(self.app) self.assertEqual(w.get('/').status_code, 200) original_server_name = self.app.config['SERVER_NAME'] try: self.app.config['SERVER_NAME'] = 'webtest-app.local' w = TestApp(self.app) self.assertEqual(w.get('/').status_code, 200) finally: self.app.config['SERVER_NAME'] = original_server_name
class WebTestMixin(object): def create_app(self): config = os.environ.get('KOZMIC_CONFIG', 'kozmic.config.TestingConfig') return create_app(config) def setup_app_and_ctx(self): self.app = self.create_app() self.ctx = self.app.app_context() self.ctx.push() self.w = TestApp(self.app) def teardown_app_and_ctx(self): self.ctx.pop() def login(self, user_id): with self.w.session_transaction() as sess: sess['user_id'] = user_id
class WebTestMixin(object): def create_app(self): config = os.environ.get('KOZMIC_CONFIG', 'kozmic.config.TestingConfig') return create_app(config) def setup_app_and_ctx(self): self.app = self.create_app() self.ctx = self.app.app_context() self.ctx.push() self.w = TestApp(self.app) def teardown_app_and_ctx(self): self.ctx.pop() def login(self, user_id): with self.w.session_transaction() as sess: sess['user_id'] = user_id
class TestMainFeatures(unittest.TestCase): def setUp(self): self.app = create_app() self.w = TestApp(self.app) def test_single_template(self): r = self.w.get('/') self.assertFalse(r.flashes) self.assertEqual(len(r.contexts), 1) self.assertEqual(r.context['text'], 'Hello!') self.assertEqual(r.template, 'template.html') self.assertNotIn('qwerty', r.session) def test_two_templates_and_flash_messages(self): r = self.w.get('/').form.submit() self.assertEqual(len(r.contexts), 2) if flask_gte_0_10: self.assertEqual(len(r.flashes), 2) category, message = r.flashes[0] self.assertEqual(message, 'You have pressed "Quit"...') category, message = r.flashes[1] self.assertEqual(message, 'Flash message that will never be shown') else: self.assertEqual(len(r.flashes), 1) category, message = r.flashes[0] self.assertEqual(message, 'You have pressed "Quit"...') with self.assertRaises(AssertionError): r.context # Because there are more than one used templates self.assertEqual( r.contexts['template.html']['text'], 'Goodbye!') self.assertEqual( r.contexts['extra-template.html']['extra_text'], 'Some text.') def test_session_transaction(self): r = self.w.get('/whoami/') self.assertEqual(r.body, 'nobody') with self.w.session_transaction() as sess: sess['username'] = '******' r = self.w.get('/whoami/') self.assertEqual(r.session['username'], 'aromanovich') self.assertEqual(r.body, 'aromanovich') def test_init(self): w = TestApp(self.app) self.assertEqual(w.get('/').status_code, 200) original_server_name = self.app.config['SERVER_NAME'] try: self.app.config['SERVER_NAME'] = 'webtest-app.local' w = TestApp(self.app) self.assertEqual(w.get('/').status_code, 200) finally: self.app.config['SERVER_NAME'] = original_server_name