1
- from django . test import SimpleTestCase , override_settings
1
+ import pytest
2
2
from testapp .views import ObjectionException
3
3
4
4
from django_prometheus .testutils import (
@@ -24,21 +24,24 @@ def T(metric_name):
24
24
return "%s_total" % M (metric_name )
25
25
26
26
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 :
29
28
"""Test django_prometheus.middleware.
30
29
31
30
Note that counters related to exceptions can't be tested as
32
31
Django's test Client only simulates requests and the exception
33
32
handling flow is very different in that simulation.
34
33
"""
35
34
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 ):
37
40
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" })
42
45
43
46
assert_metric_diff (registry , 4 , M ("requests_before_middlewares_total" ))
44
47
assert_metric_diff (registry , 4 , M ("responses_before_middlewares_total" ))
@@ -83,7 +86,7 @@ def test_request_counters(self):
83
86
assert_metric_diff (registry , 4 , T ("responses_total_by_charset" ), charset = "utf-8" )
84
87
assert_metric_diff (registry , 0 , M ("responses_streaming_total" ))
85
88
86
- def test_latency_histograms (self ):
89
+ def test_latency_histograms (self , client ):
87
90
# Caution: this test is timing-based. This is not ideal. It
88
91
# runs slowly (each request to /slow takes at least .1 seconds
89
92
# to complete), to eliminate flakiness we adjust the buckets used
@@ -93,7 +96,7 @@ def test_latency_histograms(self):
93
96
94
97
# This always takes more than .1 second, so checking the lower
95
98
# buckets is fine.
96
- self . client .get ("/slow" )
99
+ client .get ("/slow" )
97
100
assert_metric_diff (
98
101
registry ,
99
102
0 ,
@@ -111,11 +114,11 @@ def test_latency_histograms(self):
111
114
method = "GET" ,
112
115
)
113
116
114
- def test_exception_latency_histograms (self ):
117
+ def test_exception_latency_histograms (self , client ):
115
118
registry = save_registry ()
116
119
117
120
try :
118
- self . client .get ("/objection" )
121
+ client .get ("/objection" )
119
122
except ObjectionException :
120
123
pass
121
124
assert_metric_diff (
@@ -127,9 +130,9 @@ def test_exception_latency_histograms(self):
127
130
method = "GET" ,
128
131
)
129
132
130
- def test_streaming_responses (self ):
133
+ def test_streaming_responses (self , client ):
131
134
registry = save_registry ()
132
- self . client .get ("/" )
133
- self . client .get ("/file" )
135
+ client .get ("/" )
136
+ client .get ("/file" )
134
137
assert_metric_diff (registry , 1 , M ("responses_streaming_total" ))
135
138
assert_metric_diff (registry , 1 , M ("responses_body_total_bytes_bucket" ), le = "+Inf" )
0 commit comments