tests/conftest.py

This commit is contained in:
JensDiemer 2022-09-18 17:52:34 +02:00
parent 9670b2ec6a
commit 1133274b75
4 changed files with 45 additions and 28 deletions

View file

@ -45,7 +45,7 @@ tox: check-poetry ## Run pytest via tox with all environments
poetry run tox poetry run tox
pytest: install ## Run pytest pytest: install ## Run pytest
poetry run python3 ./run_pytest.py poetry run pytest
local-test: install ## Run local_test.py to run the project locally local-test: install ## Run local_test.py to run the project locally
poetry run python3 ./local_test.py poetry run python3 ./local_test.py

View file

@ -16,8 +16,6 @@ if settings.PATH_URL:
# TODO: https://github.com/jedie/django-for-runners/issues/25 # TODO: https://github.com/jedie/django-for-runners/issues/25
# path(settings.MEDIA_URL.lstrip('/'), include('django_tools.serve_media_app.urls')), # path(settings.MEDIA_URL.lstrip('/'), include('django_tools.serve_media_app.urls')),
] ]
if settings.SERVE_FILES:
urlpatterns += static.static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
else: else:
# Installed to domain root, without a path prefix # Installed to domain root, without a path prefix
# Just use the default project urls.py # Just use the default project urls.py

View file

@ -1,25 +0,0 @@
"""
Run pytest against local test creation
"""
from pathlib import Path
try:
from django_yunohost_integration.pytest_helper import run_pytest
except ImportError as err:
raise ImportError('Did you forget to activate a virtual environment?') from err
BASE_PATH = Path(__file__).parent
def main():
run_pytest(
django_settings_path=BASE_PATH / 'conf' / 'settings.py',
destination=BASE_PATH / 'local_test',
)
if __name__ == '__main__':
main()

44
tests/conftest.py Normal file
View file

@ -0,0 +1,44 @@
"""
Special pytest init:
- Build a "local_test" YunoHost installation
- init Django with this local test installation
So the pytests will run against this local test installation
"""
import os
import sys
from pathlib import Path
import django
from django_yunohost_integration.local_test import create_local_test
BASE_PATH = Path(__file__).parent.parent
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
def pytest_configure():
print('Compile YunoHost files...')
final_path = create_local_test(
django_settings_path=BASE_PATH / 'conf' / 'settings.py',
destination=BASE_PATH / 'local_test',
runserver=False,
extra_replacements={
'__DEBUG_ENABLED__': '0',
'__LOG_LEVEL__': 'INFO',
'__ADMIN_EMAIL__': 'foo-bar@test.tld',
'__DEFAULT_FROM_EMAIL__': 'django_app@test.tld',
},
)
print('Local test files created here:')
print(f'"{final_path}"')
os.chdir(final_path)
final_home_str = str(final_path)
if final_home_str not in sys.path:
sys.path.insert(0, final_home_str)
django.setup()