Skip to content

Commit 5bd444a

Browse files
authored
PYTHON-3917 Fix handling of warnings in connection string tests (#1357)
1 parent 42c0841 commit 5bd444a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

test/test_uri_spec.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def setUp(self):
7373
clear_warning_registry()
7474

7575

76-
def get_error_message_template(expected, artefact):
77-
return "{} {} for test '{}'".format("Expected" if expected else "Unexpected", artefact, "%s")
76+
def get_error_message_template(expected, artifact):
77+
return "{} {} for test '{}'".format("Expected" if expected else "Unexpected", artifact, "%s")
7878

7979

8080
def run_scenario_in_dir(target_workdir):
@@ -99,25 +99,27 @@ def run_scenario(self):
9999
self.skipTest("This test needs dnspython package.")
100100
valid = True
101101
warning = False
102+
expected_warning = test.get("warning", False)
103+
expected_valid = test.get("valid", True)
102104

103105
with warnings.catch_warnings(record=True) as ctx:
104-
warnings.simplefilter("always")
106+
warnings.simplefilter("ignore", category=ResourceWarning)
105107
try:
106108
options = parse_uri(test["uri"], warn=True)
107109
except Exception:
108110
valid = False
109111
else:
110112
warning = len(ctx) > 0
113+
if expected_valid and warning and not expected_warning:
114+
raise ValueError("Got unexpected warning(s): ", [str(i) for i in ctx])
111115

112-
expected_valid = test.get("valid", True)
113116
self.assertEqual(
114117
valid,
115118
expected_valid,
116119
get_error_message_template(not expected_valid, "error") % test["description"],
117120
)
118121

119122
if expected_valid:
120-
expected_warning = test.get("warning", False)
121123
self.assertEqual(
122124
warning,
123125
expected_warning,

0 commit comments

Comments
 (0)