How to test view mixins in isolation?

Hello,

I wrote an GetArchivedMixin that filters queryset based on value of GET parameter. I would like to test it without rellying on other parts of codebase - so with isolated test-only models, templates, urls and views. My ideal scenario is something like tests/generic_views in Django codebase.

How can I configure tests in this way? I would greatly appreciate if someone could point me to correct place in documentation for this or to some guide.

I tried creating tests/ directory inside app directory with test_GetArchivedMixin.py, models.py and empty __init__.py file. Running python manage.py test successfully found this test, but logically failed with django.db.utils.OperationalError: no such table: core_simplemodel.

test_GetArchivedMixin.py:

from typing import Any

from core.views.mixins import GetArchivedMixin
from django.http import HttpRequest, HttpResponse
from django.test import RequestFactory, TestCase
from django.views.generic import ListView

from .models import SimpleModel


class GetArchivedListView(ListView, GetArchivedMixin):
    queryset = SimpleModel.objects.all()

    def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
        return HttpResponse(status=200, content=self.get_queryset().all())


class GetArchivedViewTests(TestCase):
    def setUp(self) -> None:
        self.factory = RequestFactory()

    @classmethod
    def setUpTestData(cls) -> None:
        cls.naziv1: SimpleModel = SimpleModel.objects.create(naziv="Naziv1")

        cls.naziv1.is_active = False
        cls.naziv1.save()

    def test_all(self):
        request = self.factory.get("")

        response = GetArchivedListView.as_view()(request)

        self.assertEqual(response.status_code, 200)

models.py

from core.models.generic import ArchiveableModel
from django.db import models


class SimpleModel(ArchiveableModel):
    naziv = models.CharField(max_length=100)

Thank you and kind regards!

Welcome @MatijaSi !

As a side note, I just want to point out that your class definition here is incorrect:

When adding mixins to base CBVs, the mixins should be listed first. This definition should be:
class GetArchivedListView(GetArchivedMixin, ListView):

On another note, the error being reported:

is telling you that the model doesn’t exist in the database you’re using for your tests, and that you may need to run migrate for it.

class GetArchivedListView(ListView, GetArchivedMixin):

Nice catch, thank you - but I think this doesn’t pertain to the main issue.

is telling you that the model doesn’t exist in the database you’re using for your tests, and that you may need to run migrate for it.

Thats the point - my question is basically how to configure tests to use different models and migrations than rest of the project (test-only models?).

Thank you

Thinking a bit more about this: viable solution could be making a new app testapp and including it in test only settings, then configuring manage.py to use test settings by default when running tests. Any obvious pitfalls related to this I am missing?