Adds does_not_raise context manager

Addressing issues #4324 and #1830
This commit is contained in:
Arel Cordero
2019-01-24 21:53:14 +00:00
parent 16f8cdac95
commit afe9fd5ffd
4 changed files with 73 additions and 0 deletions

View File

@@ -94,6 +94,46 @@ class TestRaises(object):
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*3 passed*"])
def test_does_not_raise(self, testdir):
testdir.makepyfile(
"""
import pytest
import _pytest._code
@pytest.mark.parametrize('example_input,expectation', [
(3, pytest.does_not_raise()),
(2, pytest.does_not_raise()),
(1, pytest.does_not_raise()),
(0, pytest.raises(ZeroDivisionError)),
])
def test_division(example_input, expectation):
'''Test how much I know division.'''
with expectation:
assert (6 / example_input) is not None
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*4 passed*"])
def test_does_not_raise_does_raise(self, testdir):
testdir.makepyfile(
"""
import pytest
import _pytest._code
@pytest.mark.parametrize('example_input,expectation', [
(0, pytest.does_not_raise()),
(1, pytest.raises(ZeroDivisionError)),
])
def test_division(example_input, expectation):
'''Test how much I know division.'''
with expectation:
assert (6 / example_input) is not None
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*2 failed*"])
def test_noclass(self):
with pytest.raises(TypeError):
pytest.raises("wrong", lambda: None)