From 493530ec6dea82041ed30d3fba6debc7e76a8283 Mon Sep 17 00:00:00 2001 From: Demian Brecht Date: Mon, 5 Oct 2015 12:13:25 -0700 Subject: [PATCH] typo fix, changed position of context manager notes --- _pytest/python.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index 311ed6ec7..32cf82668 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1194,6 +1194,28 @@ def raises(expected_exception, *args, **kwargs): >>> with raises(ZeroDivisionError): ... 1/0 + .. note:: + + When using ``pytest.raises`` as a context manager, it's worthwhile to + note that normal context manager rules apply and that the exception + raised *must* be the final line in the scope of the context manager. + Lines of code after that, within the scope of the context manager will + not be executed. For example:: + + >>> with raises(OSError) as err: + assert 1 == 1 # this will execute as expected + raise OSError(errno.EEXISTS, 'directory exists') + assert err.errno == errno.EEXISTS # this will not execute + + Instead, the following approach must be taken (note the difference in + scope):: + + >>> with raises(OSError) as err: + assert 1 == 1 # this will execute as expected + raise OSError(errno.EEXISTS, 'directory exists') + + assert err.errno == errno.EEXISTS # this will now execute + Or you can specify a callable by passing a to-be-called lambda:: >>> raises(ZeroDivisionError, lambda: 1/0) @@ -1213,28 +1235,6 @@ def raises(expected_exception, *args, **kwargs): >>> raises(ZeroDivisionError, "f(0)") - .. note:: - - When using ``pytest.raises`` as a context manager, it's worthwhile to - note that normal context manager rules apply and that the exception - raised *must* be the final line in the scope of the context manager. - Lines of code after that, within the scope of the context manager will - not be executed. For example:: - - >>> with raises(OSError) as err: - assert 1 == 1 # this will execute as expected - raise OSError(errno.EEXISTS, 'directory exists') - assert err.errno = errno.EEXISTS # this will not execute - - Instead, the following approach must be taken (note the difference in - scope):: - - >>> with raises(OSError) as err: - assert 1 == 1 # this will execute as expected - raise OSError(errno.EEXISTS, 'directory exists') - - assert err.errno = errno.EEXISTS # this will now execute - Performance note: -----------------