conftest.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. """Defines fixtures available to all tests."""
  3. import pytest
  4. from walle.app import create_app
  5. from walle.config.settings_test import TestConfig
  6. from walle.model.database import db as _db
  7. from webtest import TestApp
  8. from .factories import UserFactory
  9. @pytest.yield_fixture(scope='session')
  10. def app():
  11. """An application for the tests."""
  12. _app = create_app(TestConfig)
  13. # _app.config['LOGIN_DISABLED'] = True
  14. _app.login_manager.init_app(_app)
  15. ctx = _app.test_request_context()
  16. ctx.push()
  17. yield _app
  18. ctx.pop()
  19. @pytest.yield_fixture(scope='session')
  20. def client(app):
  21. """A Flask test client. An instance of :class:`flask.testing.TestClient`
  22. by default.
  23. """
  24. with app.test_client() as client:
  25. yield client
  26. @pytest.fixture(scope='session')
  27. def testapp(app):
  28. """A Webtest app."""
  29. return TestApp(app)
  30. @pytest.yield_fixture(scope='session')
  31. def db(app):
  32. """A database for the tests."""
  33. _db.app = app
  34. with app.app_context():
  35. _db.create_all()
  36. yield _db
  37. # Explicitly close DB connection
  38. _db.session.close()
  39. _db.drop_all()
  40. @pytest.fixture(scope='session')
  41. def user(db):
  42. """A user for the tests."""
  43. user = UserFactory()
  44. db.session.commit()
  45. return user