From 1bb3443237843ff25ef46039c2d347d30f122e9b Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Mon, 11 May 2026 00:17:32 +0545 Subject: [PATCH] feat(src/djinit/templates): scaffold pytest fixtures and configuration templates- New Features - Added conftest.py template with Django test database, API client, and test user fixtures. - Added pytest.ini template configuring settings, test discovery, and custom markers. - Integrated pytest and coverage requirements into the project requirements template. - Enhancements - None - Bug Fixes - None --- src/djinit/templates/project/conftest.py-tpl | 46 +++++++++++++++++++ src/djinit/templates/project/pytest.ini-tpl | 14 ++++++ src/djinit/templates/project/requirements-tpl | 3 ++ 3 files changed, 63 insertions(+) create mode 100644 src/djinit/templates/project/conftest.py-tpl create mode 100644 src/djinit/templates/project/pytest.ini-tpl diff --git a/src/djinit/templates/project/conftest.py-tpl b/src/djinit/templates/project/conftest.py-tpl new file mode 100644 index 0000000..a07c40f --- /dev/null +++ b/src/djinit/templates/project/conftest.py-tpl @@ -0,0 +1,46 @@ +#--------------------------------------------------# +# The following was generated by djinit: # +#--------------------------------------------------# + +import os +import sys +import django +from pathlib import Path + +# Add project to path +sys.path.insert(0, str(Path(__file__).parent.parent)) + +# Configure Django settings +os.environ.setdefault('DJANGO_SETTINGS_MODULE', '[[ module_name ]].settings.development') +django.setup() + +import pytest + + +@pytest.fixture(scope="session") +def django_db_setup(): + """Setup test database.""" + from django.conf import settings + settings.DATABASES['default'] = { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:', + } + + +@pytest.fixture +def api_client(): + """Return a test client.""" + from rest_framework.test import APIClient + return APIClient() + + +@pytest.fixture +def user(db): + """Create a test user.""" + from django.contrib.auth import get_user_model + User = get_user_model() + return User.objects.create_user( + username='testuser', + email='test@example.com', + password='testpass123' + ) \ No newline at end of file diff --git a/src/djinit/templates/project/pytest.ini-tpl b/src/djinit/templates/project/pytest.ini-tpl new file mode 100644 index 0000000..55ce09d --- /dev/null +++ b/src/djinit/templates/project/pytest.ini-tpl @@ -0,0 +1,14 @@ +#--------------------------------------------------# +# The following was generated by djinit: # +#--------------------------------------------------# + +[pytest] +DJANGO_SETTINGS_MODULE = [[ module_name ]].settings.development +python_files = tests.py test_*.py *_tests.py +python_classes = Test* *Tests *Test +python_functions = test_* +addopts = --strict-markers --tb=short +testpaths = [[ app_names ]] +markers = + slow: marks tests as slow (deselect with '-m "not slow"') + integration: marks tests as integration tests \ No newline at end of file diff --git a/src/djinit/templates/project/requirements-tpl b/src/djinit/templates/project/requirements-tpl index 9dbb655..bad8209 100644 --- a/src/djinit/templates/project/requirements-tpl +++ b/src/djinit/templates/project/requirements-tpl @@ -13,6 +13,9 @@ whitenoise django-tailwind-cli # @IF use_tailwind django-htmx # @IF use_htmx django-vite # @IF use_vite +pytest # @IF use_pytest +pytest-django # @IF use_pytest +pytest-cov # @IF use_pytest # @IF database_type == 'mysql' mysqlclient # @ELSE