[Refactor] Move test fixtures to conftest.py

…so they get automatically loaded
This commit is contained in:
Gergely Polonkai 2018-07-25 20:02:28 +02:00
parent 1e1e085ba4
commit bc67e692e0
6 changed files with 66 additions and 40 deletions

62
tests/conftest.py Normal file
View File

@ -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()

View File

@ -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 = {}

View File

@ -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
"""

View File

@ -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):

View File

@ -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):

View File

@ -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):