Repository: pytest-dev/pytest-django

Issue:
Add Django version to report header
When pytest is run, it displays all the versions of the platform and plugins in the report header. Currently the `pytest-django` report header only adds the Django settings and (if found) configuration from `django-configuration`. It would be very useful if `pytest-django` also included the version of Django.

I recently had to debug an issue where I was swapping between two versions of Django and, while it is a small thing, having this info when running the tests would have been a nice DX improvement.

This should be an easy patch to write. Opening this issue to gauge interest.

(I searched the issues for a similar request, but did not find any. Apologies if this has been brought up before.)


Discussion:
I think it would be nice!

Resolving patch:
diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py
--- a/pytest_django/plugin.py
+++ b/pytest_django/plugin.py
@@ -367,6 +367,11 @@ def pytest_configure() -> None:
 @pytest.hookimpl()
 def pytest_report_header(config: pytest.Config) -> Optional[List[str]]:
     report_header = config.stash[report_header_key]
+
+    if "django" in sys.modules:
+        import django
+        report_header.insert(0, f"version: {django.get_version()}")
+
     if report_header:
         return ["django: " + ", ".join(report_header)]
     return None


Test patch:
diff --git a/tests/test_django_configurations.py b/tests/test_django_configurations.py
--- a/tests/test_django_configurations.py
+++ b/tests/test_django_configurations.py
@@ -42,7 +42,8 @@ def test_settings():
     )
     result = pytester.runpytest_subprocess()
     result.stdout.fnmatch_lines([
-        'django: settings: tpkg.settings_env (from env), configuration: MySettings (from env)',
+        "django: version: *, settings: tpkg.settings_env (from env), "
+        "configuration: MySettings (from env)",
         "* 1 passed*",
     ])
     assert result.ret == 0
@@ -73,7 +74,8 @@ def test_ds():
     )
     result = pytester.runpytest_subprocess()
     result.stdout.fnmatch_lines([
-        'django: settings: tpkg.settings_env (from env), configuration: MySettings (from env)',
+        "django: version: *, settings: tpkg.settings_env (from env), "
+        "configuration: MySettings (from env)",
         "* 1 passed*",
     ])
     assert result.ret == 0
@@ -103,7 +105,8 @@ def test_ds():
     )
     result = pytester.runpytest_subprocess()
     result.stdout.fnmatch_lines([
-        'django: settings: tpkg.settings_ini (from ini), configuration: MySettings (from ini)',
+        "django: version: *, settings: tpkg.settings_ini (from ini), "
+        "configuration: MySettings (from ini)",
         "* 1 passed*",
     ])
     assert result.ret == 0
@@ -134,7 +137,7 @@ def test_ds():
     )
     result = pytester.runpytest_subprocess("--ds=tpkg.settings_opt", "--dc=MySettings")
     result.stdout.fnmatch_lines([
-        'django: settings: tpkg.settings_opt (from option),'
+        'django: version: *, settings: tpkg.settings_opt (from option),'
         ' configuration: MySettings (from option)',
         "* 1 passed*",
     ])
diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py
--- a/tests/test_django_settings_module.py
+++ b/tests/test_django_settings_module.py
@@ -38,7 +38,7 @@ def test_ds():
     )
     result = pytester.runpytest_subprocess()
     result.stdout.fnmatch_lines([
-        "django: settings: tpkg.settings_ini (from ini)",
+        "django: version: *, settings: tpkg.settings_ini (from ini)",
         "*= 1 passed*",
     ])
     assert result.ret == 0
@@ -59,7 +59,7 @@ def test_settings():
     )
     result = pytester.runpytest_subprocess()
     result.stdout.fnmatch_lines([
-        "django: settings: tpkg.settings_env (from env)",
+        "django: version: *, settings: tpkg.settings_env (from env)",
         "*= 1 passed*",
     ])
 
@@ -85,7 +85,7 @@ def test_ds():
     )
     result = pytester.runpytest_subprocess("--ds=tpkg.settings_opt")
     result.stdout.fnmatch_lines([
-        "django: settings: tpkg.settings_opt (from option)",
+        "django: version: *, settings: tpkg.settings_opt (from option)",
         "*= 1 passed*",
     ])