diff --git a/docs/changelog.rst b/docs/changelog.rst index e1c8a982..75fa1cf1 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -9,6 +9,12 @@ Compatibility * Official Django 6.1 support. +Improvements +^^^^^^^^^^^^ + +* Added :fixture:`request_factory` as a readable alias for :fixture:`rf` + (`#1252 `__). + v4.14.0 (2026-08-10) -------------------- diff --git a/docs/helpers.rst b/docs/helpers.rst index 688458fb..5e21a5e3 100644 --- a/docs/helpers.rst +++ b/docs/helpers.rst @@ -187,6 +187,8 @@ More information on fixtures is available in the :ref:`pytest documentation An instance of a :class:`django.test.RequestFactory`. +Also available as :fixture:`request_factory`. + Example """"""" @@ -204,6 +206,22 @@ Example response = my_view(request) assert response.status_code == 200 +.. fixture:: request_factory + +``request_factory`` - ``RequestFactory`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +A readable alias for :fixture:`rf`. Both fixtures return the same +:class:`django.test.RequestFactory` instance within a test, including when +``rf`` is overridden by a custom fixture. + +Example:: + + def test_request(request_factory): + request = request_factory.get('/example/', {'name': 'pytest-django'}) + assert request.path == '/example/' + assert request.GET['name'] == 'pytest-django' + .. fixture:: async_rf ``async_rf`` - ``AsyncRequestFactory`` diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 2acf0e2f..3f52600a 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -47,6 +47,7 @@ "django_user_model", "django_username_field", "live_server", + "request_factory", "rf", "settings", "transactional_db", @@ -538,6 +539,12 @@ def rf() -> django.test.RequestFactory: return RequestFactory() +@pytest.fixture +def request_factory(rf: django.test.RequestFactory) -> django.test.RequestFactory: + """Alias for the rf fixture.""" + return rf + + @pytest.fixture def async_rf() -> django.test.AsyncRequestFactory: """AsyncRequestFactory instance""" diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index aa45ff38..9f338525 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -46,6 +46,7 @@ django_user_model, # noqa: F401 django_username_field, # noqa: F401 live_server, # noqa: F401 + request_factory, # noqa: F401 rf, # noqa: F401 settings, # noqa: F401 transactional_db, # noqa: F401 diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 5626de01..9e0c3138 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -102,6 +102,18 @@ def test_rf(rf: RequestFactory) -> None: assert isinstance(rf, RequestFactory) +def test_request_factory(request_factory: RequestFactory) -> None: + assert isinstance(request_factory, RequestFactory) + request = request_factory.get("/example/", {"name": "pytest-django"}) + assert request.method == "GET" + assert request.path == "/example/" + assert request.GET["name"] == "pytest-django" + + +def test_request_factory_alias(rf: RequestFactory, request_factory: RequestFactory) -> None: + assert request_factory is rf + + def test_async_rf(async_rf: AsyncRequestFactory) -> None: assert isinstance(async_rf, AsyncRequestFactory)