.cursor/skills/pytest-django-patterns/SKILL.md
pytest-django testing patterns, Factory Boy, fixtures, and TDD workflow. Use when writing tests, creating test factories, or following TDD red-green-refactor cycle.
npx skillsauth add AngelDann/app-comisions-dist pytest-django-patternsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Always follow this cycle:
Critical rule: If implementing a feature or fixing a bug, write the test BEFORE touching production code.
@pytest.mark.django_db on any test touching the databasepytestmark = pytest.mark.django_dbUse Factory Boy for models, pytest fixtures for setup:
Factories: Create model instances with realistic data (UserFactory())
factory.Sequence() for unique fieldsfactory.Faker() for realistic fake datafactory.SubFactory() for foreign keys@factory.post_generation for M2M relationshipsFixtures: Setup clients, auth state, or shared resources
client fixture: Django test clientauth_client fixture: client.force_login(user) for authenticated requestsconftest.py for reuse across test filesStructure tests to mirror app structure:
tests/
├── apps/
│ └── posts/
│ ├── test_models.py
│ ├── test_views.py
│ └── test_forms.py
├── factories.py
└── conftest.py
Group related tests in classes:
TestComponentName (e.g., TestPostListView)test_<action>_<expected_outcome>@pytest.mark.parametrize for testing multiple scenariosHTTP_HX_REQUEST header returns partial template__str__, custom methods return expected valuesCheck partial template rendered when HX-Request header present:
HTTP_HX_REQUEST="true" to client requestresponse.templates contains partial template nameCreate authenticated vs anonymous client fixtures:
Verify efficient queries:
select_related()/prefetch_related()Pass instance to form for updates:
form = MyForm(data=new_data, instance=existing_obj)form.save() updates, doesn't createParametrize multiple scenarios:
Use @pytest.mark.parametrize("input,expected", [...]) for testing various inputs
Mock external services:
Use mocker.patch() to avoid actual HTTP calls, emails, file operations
Check database changes:
Model.objects.filter(...).exists() after creationModel.objects.count() == expected for deletionsrefresh_from_db() to verify updatesTest error handling:
uv run pytest # All tests
uv run pytest -x # Stop on first failure
uv run pytest --lf # Run last failed
uv run pytest -x --lf # Stop first, last failed only
uv run pytest -k "test_name" # Run tests matching pattern
uv run pytest tests/apps/posts/ # Specific directory
uv run pytest --cov=apps # With coverage report
Without uv, use pytest with the same flags after activating the virtual environment.
@pytest.mark.django_db: Results in "Database access not allowed" errorsIn pyproject.toml:
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "config.settings.test"
python_files = ["test_*.py"]
addopts = ["--reuse-db", "-ra"]
Adjust DJANGO_SETTINGS_MODULE to this project's test settings (e.g. fito_ai.settings or a dedicated test module).
In conftest.py:
Define shared fixtures (auth_client, common factories, etc.)
Adapted from claude-code-django (.claude/skills/pytest-django-patterns).
development
HTMX patterns for Django including partial templates, hx-* attributes, and dynamic UI without JavaScript. Use when building interactive UI, handling AJAX requests, or creating dynamic components.
development
Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
development
Check if documentation is in sync with code. Use when the user wants to verify that documentation matches current code, find outdated docs, or audit documentation accuracy. Triggers on requests like "check docs", "sync documentation", "are the docs up to date", "/docs-sync".
development
Django template patterns including inheritance, partials, tags, and filters. Use when working with templates, creating reusable components, or organizing template structure.