Skip to content

Commit 3760236

Browse files
committed
Change metric used to determine if pytest is being used
1 parent a7ec25a commit 3760236

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

ansible_base/lib/utils/settings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importlib
22
import logging
3+
import os
34
from typing import Any
45

56
from django.conf import settings
@@ -80,3 +81,13 @@ def is_aoc_instance():
8081
except ValueError:
8182
logger.error(f'{managed_cloud_setting} was set but could not be converted to a boolean, assuming false')
8283
return False
84+
85+
86+
def is_testing() -> bool:
87+
# Present during setup/call/teardown of tests
88+
if os.environ.get("PYTEST_CURRENT_TEST"):
89+
return True
90+
# Present for xdist workers for the whole session
91+
if os.environ.get("PYTEST_XDIST_WORKER"):
92+
return True
93+
return False

ansible_base/resource_registry/models/service_identifier.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import sys
21
import uuid
32

43
from django.conf import settings
54
from django.db import IntegrityError, models, transaction
65

6+
from ansible_base.lib.utils.settings import is_testing
7+
78

89
class ServiceID(models.Model):
910
"""
@@ -27,7 +28,7 @@ def service_id():
2728
if not _service_id:
2829
obj = ServiceID.objects.first()
2930
if obj is None:
30-
if settings.DEBUG or "pytest" in sys.argv:
31+
if settings.DEBUG or is_testing():
3132
try:
3233
with transaction.atomic():
3334
obj = ServiceID.objects.create()

test_app/tests/lib/utils/test_settings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.test import override_settings
55

66
from ansible_base.lib.dynamic_config.settings_logic import get_dab_settings
7-
from ansible_base.lib.utils.settings import SettingNotSetException, get_setting, is_aoc_instance
7+
from ansible_base.lib.utils.settings import SettingNotSetException, get_setting, is_aoc_instance, is_testing
88

99

1010
@pytest.mark.django_db
@@ -85,3 +85,8 @@ def test_fallback_cache():
8585
get_dab_settings([], caches=fallback_cache)
8686

8787
assert 'CACHES' not in get_dab_settings([], caches=None)
88+
89+
90+
def test_is_testing():
91+
# Generated by Claude Code (claude-sonnet-4-5@20250929)
92+
assert is_testing() is True

0 commit comments

Comments
 (0)