conftest.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding:utf-8 -*-
  2. import pytest
  3. import flask_migrate
  4. from app import create_app
  5. from extensions.ext_database import db
  6. @pytest.fixture(scope='module')
  7. def test_client():
  8. # Create a Flask app configured for testing
  9. from config import TestConfig
  10. flask_app = create_app(TestConfig())
  11. flask_app.config.from_object('config.TestingConfig')
  12. # Create a test client using the Flask application configured for testing
  13. with flask_app.test_client() as testing_client:
  14. # Establish an application context
  15. with flask_app.app_context():
  16. yield testing_client # this is where the testing happens!
  17. @pytest.fixture(scope='module')
  18. def init_database(test_client):
  19. # Initialize the database
  20. with test_client.application.app_context():
  21. flask_migrate.upgrade()
  22. yield # this is where the testing happens!
  23. # Clean up the database
  24. with test_client.application.app_context():
  25. flask_migrate.downgrade()
  26. @pytest.fixture(scope='module')
  27. def db_session(test_client):
  28. with test_client.application.app_context():
  29. yield db.session
  30. @pytest.fixture(scope='function')
  31. def login_default_user(test_client):
  32. # todo
  33. yield # this is where the testing happens!
  34. test_client.get('/logout', follow_redirects=True)