From ff22b893f1136bb3650a4fc492a2102dcf14a936 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Thu, 24 Oct 2024 23:36:09 +0200 Subject: [PATCH] Experimental fixture --- src/tests/conftest.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/tests/conftest.py b/src/tests/conftest.py index b78b02544..103312899 100644 --- a/src/tests/conftest.py +++ b/src/tests/conftest.py @@ -20,8 +20,10 @@ # . # import inspect +import os import pytest +from django.db import connection from django.test import override_settings from django.utils import translation from django_scopes import scopes_disabled @@ -113,3 +115,23 @@ def fakeredis_client(monkeypatch): redis.flushall() monkeypatch.setattr('django_redis.get_redis_connection', get_redis_connection, raising=False) yield redis + + +if os.environ.get("GITHUB_WORKFLOW", ""): + @pytest.fixture(autouse=True) + def ensure_healthy_connection(request): + # We have no idea why this is neccessary. It shouldn't be, and it costs some performance. + # However, in ~August 2024 our tests became really flake on GitHub Actions (failing more than 80% of the time) + # for no apparent reason with some error messages related to PostgreSQL connection issues. This appears to + # work around it... + + # Check if the test even has DB access + marker = request.node.get_closest_marker("django_db") + + # Run actual test + yield + + # If yes, do a dummy query at the end of the test + if marker: + with connection.cursor() as cursor: + cursor.execute("SELECT 1")