Skip to content

Commit 5c6c778

Browse files
test(starlette): Refactor shared test parametrization (getsentry#3562)
We use the same parametrization for testing FastAPI's and Starlette's `failed_request_status_codes` because the `FastApiIntegration`'s constructor is the same as `StarletteIntegration`'s constructor (the former is a subclass of the latter). Here, we refactor the test cases to define the parametrization once, then use it in both tests. This change will make some future changes simpler, since we only need to change the parameters in one place to affect the test for both frameworks.
1 parent 7e4992a commit 5c6c778

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

tests/integrations/fastapi/test_fastapi.py

+3-19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from sentry_sdk.integrations.fastapi import FastApiIntegration
1414
from sentry_sdk.integrations.starlette import StarletteIntegration
1515

16+
from tests.integrations.starlette import test_starlette
17+
1618

1719
def fastapi_app_factory():
1820
app = FastAPI()
@@ -503,25 +505,7 @@ def test_transaction_name_in_middleware(
503505
)
504506

505507

506-
@pytest.mark.parametrize(
507-
"failed_request_status_codes,status_code,expected_error",
508-
[
509-
(None, 500, True),
510-
(None, 400, False),
511-
([500, 501], 500, True),
512-
([500, 501], 401, False),
513-
([range(400, 499)], 401, True),
514-
([range(400, 499)], 500, False),
515-
([range(400, 499), range(500, 599)], 300, False),
516-
([range(400, 499), range(500, 599)], 403, True),
517-
([range(400, 499), range(500, 599)], 503, True),
518-
([range(400, 403), 500, 501], 401, True),
519-
([range(400, 403), 500, 501], 405, False),
520-
([range(400, 403), 500, 501], 501, True),
521-
([range(400, 403), 500, 501], 503, False),
522-
([None], 500, False),
523-
],
524-
)
508+
@test_starlette.parametrize_test_configurable_status_codes
525509
def test_configurable_status_codes(
526510
sentry_init,
527511
capture_events,

tests/integrations/starlette/test_starlette.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ def test_span_origin(sentry_init, capture_events):
11331133
assert span["origin"] == "auto.http.starlette"
11341134

11351135

1136-
@pytest.mark.parametrize(
1136+
parametrize_test_configurable_status_codes = pytest.mark.parametrize(
11371137
"failed_request_status_codes,status_code,expected_error",
11381138
[
11391139
(None, 500, True),
@@ -1152,6 +1152,12 @@ def test_span_origin(sentry_init, capture_events):
11521152
([None], 500, False),
11531153
],
11541154
)
1155+
"""Test cases for configurable status codes.
1156+
Also used by the FastAPI tests.
1157+
"""
1158+
1159+
1160+
@parametrize_test_configurable_status_codes
11551161
def test_configurable_status_codes(
11561162
sentry_init,
11571163
capture_events,

0 commit comments

Comments
 (0)