1- from django . test import SimpleTestCase , override_settings
1+ import pytest
22from testapp .views import ObjectionException
33
44from django_prometheus .testutils import (
@@ -24,21 +24,24 @@ def T(metric_name):
2424 return "%s_total" % M (metric_name )
2525
2626
27- @override_settings (PROMETHEUS_LATENCY_BUCKETS = (0.05 , 1.0 , 2.0 , 4.0 , 5.0 , 10.0 , float ("inf" )))
28- class TestMiddlewareMetrics (SimpleTestCase ):
27+ class TestMiddlewareMetrics :
2928 """Test django_prometheus.middleware.
3029
3130 Note that counters related to exceptions can't be tested as
3231 Django's test Client only simulates requests and the exception
3332 handling flow is very different in that simulation.
3433 """
3534
36- def test_request_counters (self ):
35+ @pytest .fixture (autouse = True )
36+ def _setup (self , settings ):
37+ settings .PROMETHEUS_LATENCY_BUCKETS = (0.05 , 1.0 , 2.0 , 4.0 , 5.0 , 10.0 , float ("inf" ))
38+
39+ def test_request_counters (self , client ):
3740 registry = save_registry ()
38- self . client .get ("/" )
39- self . client .get ("/" )
40- self . client .get ("/help" )
41- self . client .post ("/" , {"test" : "data" })
41+ client .get ("/" )
42+ client .get ("/" )
43+ client .get ("/help" )
44+ client .post ("/" , {"test" : "data" })
4245
4346 assert_metric_diff (registry , 4 , M ("requests_before_middlewares_total" ))
4447 assert_metric_diff (registry , 4 , M ("responses_before_middlewares_total" ))
@@ -83,7 +86,7 @@ def test_request_counters(self):
8386 assert_metric_diff (registry , 4 , T ("responses_total_by_charset" ), charset = "utf-8" )
8487 assert_metric_diff (registry , 0 , M ("responses_streaming_total" ))
8588
86- def test_latency_histograms (self ):
89+ def test_latency_histograms (self , client ):
8790 # Caution: this test is timing-based. This is not ideal. It
8891 # runs slowly (each request to /slow takes at least .1 seconds
8992 # to complete), to eliminate flakiness we adjust the buckets used
@@ -93,7 +96,7 @@ def test_latency_histograms(self):
9396
9497 # This always takes more than .1 second, so checking the lower
9598 # buckets is fine.
96- self . client .get ("/slow" )
99+ client .get ("/slow" )
97100 assert_metric_diff (
98101 registry ,
99102 0 ,
@@ -111,11 +114,11 @@ def test_latency_histograms(self):
111114 method = "GET" ,
112115 )
113116
114- def test_exception_latency_histograms (self ):
117+ def test_exception_latency_histograms (self , client ):
115118 registry = save_registry ()
116119
117120 try :
118- self . client .get ("/objection" )
121+ client .get ("/objection" )
119122 except ObjectionException :
120123 pass
121124 assert_metric_diff (
@@ -127,9 +130,9 @@ def test_exception_latency_histograms(self):
127130 method = "GET" ,
128131 )
129132
130- def test_streaming_responses (self ):
133+ def test_streaming_responses (self , client ):
131134 registry = save_registry ()
132- self . client .get ("/" )
133- self . client .get ("/file" )
135+ client .get ("/" )
136+ client .get ("/file" )
134137 assert_metric_diff (registry , 1 , M ("responses_streaming_total" ))
135138 assert_metric_diff (registry , 1 , M ("responses_body_total_bytes_bucket" ), le = "+Inf" )
0 commit comments