diff --git a/netbox/ipam/tests/test_api.py b/netbox/ipam/tests/test_api.py index 47b6e91ec..983787b0c 100644 --- a/netbox/ipam/tests/test_api.py +++ b/netbox/ipam/tests/test_api.py @@ -7,7 +7,7 @@ from rest_framework import status from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site from ipam.choices import * from ipam.models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF -from utilities.testing import APITestCase, choices_to_dict +from utilities.testing import APITestCase, choices_to_dict, disable_warnings class AppTest(APITestCase): @@ -1007,7 +1007,8 @@ class VLANTest(APITestCase): self.prefix1.save() url = reverse('ipam-api:vlan-detail', kwargs={'pk': self.vlan1.pk}) - response = self.client.delete(url, **self.header) + with disable_warnings('django.request'): + response = self.client.delete(url, **self.header) self.assertHttpStatus(response, status.HTTP_409_CONFLICT) diff --git a/netbox/utilities/testing.py b/netbox/utilities/testing.py index b222e497c..791eb64cb 100644 --- a/netbox/utilities/testing.py +++ b/netbox/utilities/testing.py @@ -1,3 +1,6 @@ +import logging +from contextlib import contextmanager + from django.contrib.auth.models import Permission, User from rest_framework.test import APITestCase as _APITestCase @@ -62,3 +65,15 @@ def choices_to_dict(choices_list): return { choice['value']: choice['label'] for choice in choices_list } + + +@contextmanager +def disable_warnings(logger_name): + """ + Temporarily suppress expected warning messages to keep the test output clean. + """ + logger = logging.getLogger(logger_name) + current_level = logger.level + logger.setLevel(logging.ERROR) + yield + logger.setLevel(current_level) diff --git a/netbox/utilities/tests/test_api.py b/netbox/utilities/tests/test_api.py index 3024812f9..469bb3150 100644 --- a/netbox/utilities/tests/test_api.py +++ b/netbox/utilities/tests/test_api.py @@ -9,7 +9,7 @@ from dcim.models import Region, Site from extras.choices import CustomFieldTypeChoices from extras.models import CustomField from ipam.models import VLAN -from utilities.testing import APITestCase +from utilities.testing import APITestCase, disable_warnings class WritableNestedSerializerTest(APITestCase): @@ -50,7 +50,8 @@ class WritableNestedSerializerTest(APITestCase): } url = reverse('ipam-api:vlan-list') - response = self.client.post(url, data, format='json', **self.header) + with disable_warnings('django.request'): + response = self.client.post(url, data, format='json', **self.header) self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST) self.assertEqual(VLAN.objects.count(), 0) @@ -85,7 +86,8 @@ class WritableNestedSerializerTest(APITestCase): } url = reverse('ipam-api:vlan-list') - response = self.client.post(url, data, format='json', **self.header) + with disable_warnings('django.request'): + response = self.client.post(url, data, format='json', **self.header) self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST) self.assertEqual(VLAN.objects.count(), 0) @@ -104,7 +106,8 @@ class WritableNestedSerializerTest(APITestCase): } url = reverse('ipam-api:vlan-list') - response = self.client.post(url, data, format='json', **self.header) + with disable_warnings('django.request'): + response = self.client.post(url, data, format='json', **self.header) self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST) self.assertEqual(VLAN.objects.count(), 0) @@ -119,7 +122,8 @@ class WritableNestedSerializerTest(APITestCase): } url = reverse('ipam-api:vlan-list') - response = self.client.post(url, data, format='json', **self.header) + with disable_warnings('django.request'): + response = self.client.post(url, data, format='json', **self.header) self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST) self.assertEqual(VLAN.objects.count(), 0) diff --git a/netbox/virtualization/tests/test_api.py b/netbox/virtualization/tests/test_api.py index 76f6c9d12..719954c10 100644 --- a/netbox/virtualization/tests/test_api.py +++ b/netbox/virtualization/tests/test_api.py @@ -5,7 +5,7 @@ from rest_framework import status from dcim.choices import InterfaceModeChoices from dcim.models import Interface from ipam.models import IPAddress, VLAN -from utilities.testing import APITestCase, choices_to_dict +from utilities.testing import APITestCase, choices_to_dict, disable_warnings from virtualization.choices import * from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine @@ -417,7 +417,8 @@ class VirtualMachineTest(APITestCase): } url = reverse('virtualization-api:virtualmachine-list') - response = self.client.post(url, data, format='json', **self.header) + with disable_warnings('django.request'): + response = self.client.post(url, data, format='json', **self.header) self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST) self.assertEqual(VirtualMachine.objects.count(), 4)