test_flask_client.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # coding:utf-8
  2. import unittest
  3. from flask import current_app, url_for, json
  4. from app import create_app, db
  5. class FlaskClientTest(unittest.TestCase):
  6. def setUp(self):
  7. self.app = create_app('testing')
  8. self.app_context = self.app.app_context()
  9. self.app_context.push()
  10. db.create_all()
  11. self.client = self.app.test_client()
  12. def tearDown(self):
  13. db.session.remove()
  14. db.drop_all()
  15. self.app_context.pop()
  16. def test_home_page(self):
  17. response = self.client.get(url_for('main.index'))
  18. self.assertTrue('Home' in response.get_data(as_text=True))
  19. def test_register(self):
  20. response = self.client.post(url_for('main.register'), data={
  21. 'email': '879651072@qq.com',
  22. 'name': 'Hyman',
  23. 'password1': '123',
  24. 'password2': '123'})
  25. self.assertTrue(response.status_code == 302)
  26. def test_posts(self):
  27. response = self.client.post(
  28. url_for('main.new_post'),
  29. data=json.dumps({'body': 'I am a new post'}),
  30. content_type='application/json')
  31. self.assertTrue(response.status_code == 200)