From 16495953662b48147a4d342adf846f29260c9876 Mon Sep 17 00:00:00 2001 From: Asher Foa Date: Tue, 2 May 2023 11:57:08 -0400 Subject: [PATCH] More pytest migration (#397) * More pytest migration --- .../tests/end2end/testapp/test_middleware.py | 33 ++++++++++--------- .../testapp/test_middleware_custom_labels.py | 29 ++++++++-------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/django_prometheus/tests/end2end/testapp/test_middleware.py b/django_prometheus/tests/end2end/testapp/test_middleware.py index 84619159..7b868cb5 100644 --- a/django_prometheus/tests/end2end/testapp/test_middleware.py +++ b/django_prometheus/tests/end2end/testapp/test_middleware.py @@ -1,4 +1,4 @@ -from django.test import SimpleTestCase, override_settings +import pytest from testapp.views import ObjectionException from django_prometheus.testutils import ( @@ -24,8 +24,7 @@ def T(metric_name): return "%s_total" % M(metric_name) -@override_settings(PROMETHEUS_LATENCY_BUCKETS=(0.05, 1.0, 2.0, 4.0, 5.0, 10.0, float("inf"))) -class TestMiddlewareMetrics(SimpleTestCase): +class TestMiddlewareMetrics: """Test django_prometheus.middleware. Note that counters related to exceptions can't be tested as @@ -33,12 +32,16 @@ class TestMiddlewareMetrics(SimpleTestCase): handling flow is very different in that simulation. """ - def test_request_counters(self): + @pytest.fixture(autouse=True) + def _setup(self, settings): + settings.PROMETHEUS_LATENCY_BUCKETS = (0.05, 1.0, 2.0, 4.0, 5.0, 10.0, float("inf")) + + def test_request_counters(self, client): registry = save_registry() - self.client.get("/") - self.client.get("/") - self.client.get("/help") - self.client.post("/", {"test": "data"}) + client.get("/") + client.get("/") + client.get("/help") + client.post("/", {"test": "data"}) assert_metric_diff(registry, 4, M("requests_before_middlewares_total")) assert_metric_diff(registry, 4, M("responses_before_middlewares_total")) @@ -83,7 +86,7 @@ def test_request_counters(self): assert_metric_diff(registry, 4, T("responses_total_by_charset"), charset="utf-8") assert_metric_diff(registry, 0, M("responses_streaming_total")) - def test_latency_histograms(self): + def test_latency_histograms(self, client): # Caution: this test is timing-based. This is not ideal. It # runs slowly (each request to /slow takes at least .1 seconds # to complete), to eliminate flakiness we adjust the buckets used @@ -93,7 +96,7 @@ def test_latency_histograms(self): # This always takes more than .1 second, so checking the lower # buckets is fine. - self.client.get("/slow") + client.get("/slow") assert_metric_diff( registry, 0, @@ -111,11 +114,11 @@ def test_latency_histograms(self): method="GET", ) - def test_exception_latency_histograms(self): + def test_exception_latency_histograms(self, client): registry = save_registry() try: - self.client.get("/objection") + client.get("/objection") except ObjectionException: pass assert_metric_diff( @@ -127,9 +130,9 @@ def test_exception_latency_histograms(self): method="GET", ) - def test_streaming_responses(self): + def test_streaming_responses(self, client): registry = save_registry() - self.client.get("/") - self.client.get("/file") + client.get("/") + client.get("/file") assert_metric_diff(registry, 1, M("responses_streaming_total")) assert_metric_diff(registry, 1, M("responses_body_total_bytes_bucket"), le="+Inf") diff --git a/django_prometheus/tests/end2end/testapp/test_middleware_custom_labels.py b/django_prometheus/tests/end2end/testapp/test_middleware_custom_labels.py index 6caacf51..e91badff 100644 --- a/django_prometheus/tests/end2end/testapp/test_middleware_custom_labels.py +++ b/django_prometheus/tests/end2end/testapp/test_middleware_custom_labels.py @@ -1,4 +1,4 @@ -from django.test import SimpleTestCase, override_settings +import pytest from prometheus_client import REGISTRY from prometheus_client.metrics import MetricWrapperBase from testapp.helpers import get_middleware @@ -39,28 +39,25 @@ def label_metric(self, metric, request, response=None, **labels): return super().label_metric(metric, request, response=response, **new_labels) -@override_settings( - MIDDLEWARE=get_middleware( - "testapp.test_middleware_custom_labels.AppMetricsBeforeMiddleware", - "testapp.test_middleware_custom_labels.AppMetricsAfterMiddleware", - ) -) -class TestMiddlewareMetricsWithCustomLabels(SimpleTestCase): - @classmethod - def setUpClass(cls): - super().setUpClass() +class TestMiddlewareMetricsWithCustomLabels: + @pytest.fixture(autouse=True) + def _setup(self, settings): + settings.MIDDLEWARE = get_middleware( + "testapp.test_middleware_custom_labels.AppMetricsBeforeMiddleware", + "testapp.test_middleware_custom_labels.AppMetricsAfterMiddleware", + ) # Allow CustomMetrics to be used for metric in Metrics._instance.__dict__.values(): if isinstance(metric, MetricWrapperBase): REGISTRY.unregister(metric) Metrics._instance = None - def test_request_counters(self): + def test_request_counters(self, client): registry = save_registry() - self.client.get("/") - self.client.get("/") - self.client.get("/help") - self.client.post("/", {"test": "data"}) + client.get("/") + client.get("/") + client.get("/help") + client.post("/", {"test": "data"}) assert_metric_diff(registry, 4, M("requests_before_middlewares_total")) assert_metric_diff(registry, 4, M("responses_before_middlewares_total"))