From 0826ea606bb09cb462cd2836a63fbf006a37b6af Mon Sep 17 00:00:00 2001 From: MrBlake Date: Tue, 13 Oct 2026 15:08:46 +0200 Subject: [PATCH] Fix test isolation in the shared test fixtures Fixtures were created in setUpClass, so a test that changed the shared license instance affected later tests. Use setUpTestData, which rolls back after the class and gives each test its own copy. All 20 tests pass on NetBox 4.7.2. Co-Authored-By: Claude Opus 5.5 --- netbox_slm/tests/base.py | 22 ++-------------------- netbox_slm/tests/test_functional.py | 5 ----- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/netbox_slm/tests/base.py b/netbox_slm/tests/base.py index 3a6f4c4..a8e5053 100644 --- a/netbox_slm/tests/base.py +++ b/netbox_slm/tests/base.py @@ -17,8 +17,8 @@ class SlmBaseTestCase(TestCase): software_product_installation: SoftwareProductInstallation @classmethod - def setUpClass(cls): - super().setUpClass() + def setUpTestData(cls): + # setUpTestData rolls back after the class and gives every test its own copy of these objects. manufacturer = Manufacturer.objects.create(name="test manufacturer") device_type = DeviceType.objects.create(model="test device type", manufacturer=manufacturer) device_role = DeviceRole.objects.create(name="test device role") @@ -46,21 +46,3 @@ class SlmBaseTestCase(TestCase): tenant_group=cls.tenant_group, tenant=cls.tenant, ) - - @classmethod - def tearDownClass(cls): - SoftwareLicense.objects.all().delete() - SoftwareProductInstallation.objects.all().delete() - SoftwareProductVersion.objects.all().delete() - SoftwareProduct.objects.all().delete() - Tenant.objects.all().delete() - TenantGroup.objects.all().delete() - Cluster.objects.all().delete() - ClusterType.objects.all().delete() - VirtualMachine.objects.all().delete() - Device.objects.all().delete() - Site.objects.all().delete() - DeviceRole.objects.all().delete() - DeviceType.objects.all().delete() - Manufacturer.objects.all().delete() - super().tearDownClass() diff --git a/netbox_slm/tests/test_functional.py b/netbox_slm/tests/test_functional.py index 95fb875..3123413 100644 --- a/netbox_slm/tests/test_functional.py +++ b/netbox_slm/tests/test_functional.py @@ -12,11 +12,6 @@ class FunctionalTestCase(SlmBaseTestCase): test_user, _ = get_user_model().objects.get_or_create(username="test", is_superuser=True) self.client.force_login(test_user) - @classmethod - def tearDownClass(cls): - get_user_model().objects.all().delete() - super().tearDownClass() - def test_template_content(self): cluster_response = self.client.get(f"/virtualization/clusters/{self.cluster.pk}/") self.assertContains(cluster_response, f'