Browse Source

[Refactor] Move test fixtures to conftest.py

…so they get automatically loaded
pull/111/head
Gergely Polonkai 3 years ago
parent
commit
bc67e692e0
  1. 62
      tests/conftest.py
  2. 35
      tests/helpers.py
  3. 3
      tests/test_calsocial.py
  4. 2
      tests/test_follow.py
  5. 2
      tests/test_login.py
  6. 2
      tests/test_register.py

62
tests/conftest.py

@ -0,0 +1,62 @@
# Calendar.social
# Copyright (C) 2018 Gergely Polonkai
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Helper functions and fixtures for testing
"""
from contextlib import contextmanager
import pytest
from helpers import configure_app
@pytest.fixture
def client():
"""Fixture that provides a Flask test client
"""
from calsocial import app
from calsocial.models import db
configure_app()
client = app.test_client()
with app.app_context():
db.create_all()
yield client
with app.app_context():
db.drop_all()
@pytest.fixture
def database():
"""Fixture to provide all database tables in an active application context
"""
from calsocial import app
from calsocial.models import db
configure_app()
with app.app_context():
db.create_all()
yield db
db.drop_all()

35
tests/helpers.py

@ -14,13 +14,11 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Helper functions and fixtures for testing
"""Helper functions for testing
"""
from contextlib import contextmanager
import pytest
import calsocial
from calsocial.models import db
@ -34,22 +32,6 @@ def configure_app():
calsocial.app.config['WTF_CSRF_ENABLED'] = False
@pytest.fixture
def client():
"""Fixture that provides a Flask test client
"""
configure_app()
client = calsocial.app.test_client()
with calsocial.app.app_context():
db.create_all()
yield client
with calsocial.app.app_context():
db.drop_all()
def login(client, username, password, no_redirect=False):
"""Login with the specified username and password
"""
@ -59,21 +41,6 @@ def login(client, username, password, no_redirect=False):
follow_redirects=not no_redirect)
@pytest.fixture
def database():
"""Fixture to provide all database tables in an active application context
"""
configure_app()
with calsocial.app.app_context():
db.create_all()
yield db
db.drop_all()
@contextmanager
def alter_config(app, **kwargs):
saved = {}

3
tests/test_calsocial.py

@ -17,9 +17,6 @@
"""General tests for Calendar.social
"""
from helpers import client
def test_index_no_login(client):
"""Test the main page without logging in
"""

2
tests/test_follow.py

@ -20,7 +20,7 @@
import calsocial
from calsocial.models import db, Notification, NotificationAction, Profile, User, UserFollow
from helpers import client, database, login
from helpers import login
def test_profile_follow(database):

2
tests/test_login.py

@ -20,7 +20,7 @@
import calsocial
from calsocial.models import db, User
from helpers import client, login
from helpers import login
def test_login_invalid_user(client):

2
tests/test_register.py

@ -20,7 +20,7 @@
import calsocial
from calsocial.models import db, User
from helpers import alter_config, client
from helpers import alter_config
def test_register_page(client):

Loading…
Cancel
Save