From bc67e692e03d0b5a3873d17acb24c17b070cf535 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 25 Jul 2018 20:02:28 +0200 Subject: [PATCH] [Refactor] Move test fixtures to conftest.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …so they get automatically loaded --- tests/conftest.py | 62 +++++++++++++++++++++++++++++++++++++++++ tests/helpers.py | 35 +---------------------- tests/test_calsocial.py | 3 -- tests/test_follow.py | 2 +- tests/test_login.py | 2 +- tests/test_register.py | 2 +- 6 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..51d69fc --- /dev/null +++ b/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 . + +"""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() diff --git a/tests/helpers.py b/tests/helpers.py index 971a4bd..fcfe2f1 100644 --- a/tests/helpers.py +++ b/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 . -"""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 = {} diff --git a/tests/test_calsocial.py b/tests/test_calsocial.py index ef8bd5f..f8c6cfc 100644 --- a/tests/test_calsocial.py +++ b/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 """ diff --git a/tests/test_follow.py b/tests/test_follow.py index a370a16..7679c22 100644 --- a/tests/test_follow.py +++ b/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): diff --git a/tests/test_login.py b/tests/test_login.py index 6ccab42..908984b 100644 --- a/tests/test_login.py +++ b/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): diff --git a/tests/test_register.py b/tests/test_register.py index c276b63..afef8fc 100644 --- a/tests/test_register.py +++ b/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):