Skip to content

Commit 1592d6b

Browse files
committed
Able to assert both error and warning in tandem
1 parent 4149686 commit 1592d6b

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import gc
88
from functools import wraps
99
import asyncio
10+
import warnings
1011
from test.support import import_helper
1112

1213
support.requires_working_socket(module=True)
@@ -1943,13 +1944,13 @@ def run_test(self, func, jumpFrom, jumpTo, expected, error=None,
19431944
if warning is None and error is None:
19441945
func(output)
19451946
elif warning is not None and error is None:
1946-
with self.assertWarnsRegex(*warning):
1947+
with self.assertWarnsRegex(*warning) as warning_context:
19471948
func(output)
19481949
elif warning is None and error is not None:
19491950
with self.assertRaisesRegex(*error):
19501951
func(output)
19511952
else:
1952-
with self.assertWarnsRegex(*warning) as warning_context, self.assertRaisesRegex(*error) as error_context:
1953+
with self.assertRaisesRegex(*error) as error_context, self.assertWarnsRegex(*warning) as warning_context:
19531954
func(output)
19541955

19551956
sys.settrace(None)
@@ -1970,8 +1971,8 @@ def run_async_test(self, func, jumpFrom, jumpTo, expected, error=None,
19701971
with self.assertRaisesRegex(*error):
19711972
asyncio.run(func(output))
19721973
else:
1973-
raise Exception("Not currently possible to assert both a warning and an exception in tandem, the former is seemingly ignored by the unittest framework")
1974-
assertTrue(False)
1974+
with self.assertRaisesRegex(*error) as error_context, self.assertWarnsRegex(*warning) as warning_context:
1975+
func(output)
19751976

19761977
sys.settrace(None)
19771978
asyncio.set_event_loop_policy(None)
@@ -2704,12 +2705,26 @@ class fake_function:
27042705
sys.settrace(None)
27052706
self.compare_jump_output([2, 3, 2, 3, 4], namespace["output"])
27062707

2707-
@jump_test(2, 4, [1], event='call', error=(ValueError, "can't jump from"
2708-
" the 'call' trace event of a new frame"),
2709-
warning=(RuntimeWarning, unbound_locals))
2708+
@jump_test(1, 2, [1, 2], event='call', error=(ValueError, 'Test Value Error'),
2709+
warning=(RuntimeWarning, 'Test Runtime Warning'))
2710+
def test_error_and_warning_in_tandem(output):
2711+
output.append(1)
2712+
output.append(2)
2713+
warnings.warn(RuntimeWarning("Test Runtime Warning"))
2714+
raise ValueError("Test Value Error")
2715+
2716+
@jump_test(1, 2, [1, 2], event='call', error=(ValueError, 'Test Value Error'),
2717+
warning=(RuntimeWarning, 'Test Runtime Warning'))
2718+
def test_warning_and_error_in_tandem(output):
2719+
output.append(1)
2720+
output.append(2)
2721+
raise ValueError("Test Value Error")
2722+
warnings.warn(RuntimeWarning("Test Runtime Warning"))
2723+
2724+
@jump_test(2, 3, [1], event='call', error=(ValueError, "can't jump from"
2725+
" the 'call' trace event of a new frame"))
27102726
def test_no_jump_from_call(output):
27112727
output.append(1)
2712-
x = [i for i in range(10)]
27132728
def nested():
27142729
output.append(3)
27152730
nested()

0 commit comments

Comments
 (0)