{"library":"pytest-django","code":"# myproject/pytest.ini\n[pytest]\nDJANGO_SETTINGS_MODULE = myproject.settings\npython_files = tests.py test_*.py *_tests.py\naddopts = --reuse-db\n\n# myproject/myproject/settings.py (abbreviated)\n# ... standard Django settings ...\nINSTALLED_APPS = [\n    # ...\n    'myapp',\n    # ...\n]\n\n# myproject/myapp/models.py\nfrom django.db import models\n\nclass Book(models.Model):\n    title = models.CharField(max_length=200)\n    author = models.CharField(max_length=100)\n\n    def __str__(self):\n        return self.title\n\n# myproject/myapp/tests.py\nimport pytest\nfrom myapp.models import Book\n\n@pytest.mark.django_db\ndef test_book_creation():\n    # The 'db' fixture or 'pytest.mark.django_db' is required for database access.\n    book = Book.objects.create(title='The Great Adventure', author='Jane Doe')\n    assert Book.objects.count() == 1\n    assert book.title == 'The Great Adventure'","lang":"python","description":"1. Create a `pytest.ini` file in your project root, specifying `DJANGO_SETTINGS_MODULE` to point to your Django settings file. This is crucial for pytest-django to set up the Django environment.\n2. Define your Django models as usual.\n3. Write test functions, using the `@pytest.mark.django_db` decorator or by requesting the `db` fixture for any test that interacts with the database. This enables transaction management and ensures tests are isolated.\n4. Run tests with `pytest` in your project root.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}