Skip to content

Commit

Permalink
Allow testing with pytest without manage.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AMecea committed Dec 19, 2023
1 parent 9655016 commit 5c4e98f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion colibris/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def setup():


def is_test_mode():
return isinstance(commands.get_command(), commands.test.TestCommand)
return isinstance(commands.get_command(), commands.test.TestCommand) or "pytest" in sys.argv[0]
18 changes: 18 additions & 0 deletions colibris/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os
import re
import sys
from pathlib import Path
from typing import Optional

from dotenv import load_dotenv

Expand Down Expand Up @@ -55,6 +57,15 @@ def _setup_project_package():
if m:
project_package_name = m.group(1)

# if running pytest
if project_package_name is None:
setup_file = _find_upward_file('setup.py')
if setup_file is not None:
setup_file_content = setup_file.read_text()
m = re.search(r"PROJECT_PACKAGE_NAME = \"(\w+)\"", setup_file_content)
if m:
project_package_name = m.group(1)

if project_package_name is None:
raise ImproperlyConfigured('could not identify project package name')

Expand All @@ -64,6 +75,13 @@ def _setup_project_package():
settings.PROJECT_PACKAGE_DIR = os.path.dirname(project_package.__file__)


def _find_upward_file(file_name: str) -> Optional[Path]:
for parent in Path(file_name).resolve().parents:
f = parent / file_name
if f.exists():
return f


def _setup_env():
# Try to load a default env file from the project package
path = os.path.join(settings.PROJECT_PACKAGE_DIR, ENV_DEFAULT)
Expand Down
6 changes: 6 additions & 0 deletions colibris/skeleton/__packagename__/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# include Colibiris pytest plugin
pytest_plugins = [
'colibris.test'
]

26 changes: 25 additions & 1 deletion colibris/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,36 @@

import colibris

from colibris import persist
from colibris import persist, conf
from colibris.conf import settings
from colibris.utils import import_module_or_none

from .fixtures import web_app_client


pytest_plugins = [
'aiohttp.pytest_plugin',
]


def pytest_runtest_setup():
global pytest_plugins

conf.setup()

# prevention
if 'name' in settings.DATABASE or 'name' in settings.TEST_DATABASE:
settings.DATABASE.update(settings.TEST_DATABASE)
if 'name' not in settings.TEST_DATABASE:
settings.DATABASE['name'] = 'test_' + settings.DATABASE['name']

settings.DATABASE['create'] = True # We want dbs to be created when running tests

# Use project's fixtures as a plugin so that project-specific fixtures are loaded
fixtures_path = '{}.tests.fixtures'.format(settings.PROJECT_PACKAGE_NAME)
if import_module_or_none(fixtures_path):
pytest_plugins.append(fixtures_path)

colibris.setup()
if persist.is_enabled():
manager = DatabaseManager(persist.get_database(), directory=persist.get_migrations_dir())
Expand Down

0 comments on commit 5c4e98f

Please sign in to comment.