Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ def test_raises():
with check.raises(AssertionError):
x = 3
assert 1 < x < 4

def test_raises_exception_value():
with check.raises(ValueError) as e:
raise ValueError("This is a ValueError")
check.equal(str(e.value) == "This is a ValueError")
```

## Pseudo-tracebacks
Expand Down
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All notable changes to this project be documented in this file.

### Added

- nothing so far
- `raises` returns the exception value, so the excpected error message can be verified.

### Fixed

Expand Down
7 changes: 7 additions & 0 deletions examples/test_example_raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ def test_raises_check_level_pass():
with check.raises(AssertionError):
x = 4
assert 1 < x < 3


def test_raises_exception_value():
with check.raises(ValueError) as e:
raise ValueError("This is a ValueError")

check.equal(str(e.value), "This is a ValueError")
3 changes: 3 additions & 0 deletions src/pytest_check/check_raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ class CheckRaisesContext:
def __init__(self, *expected_excs: type, msg: object = None) -> None:
self.expected_excs = expected_excs
self.msg = msg
self.value: object | None = None

def __enter__(self) -> "CheckRaisesContext":
return self

def __exit__(self, exc_type: type, exc_val: object, exc_tb: object) -> bool:
__tracebackhide__ = True

self.value = exc_val
if exc_type is not None and issubclass(exc_type, self.expected_excs):
# This is the case where an error has occured within the context,
# but it is the type we're expecting. Therefore, we return True
Expand Down
33 changes: 33 additions & 0 deletions tests/test_raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,36 @@ def test():
result = testdir.runpytest()
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(["FAILURE: hello, world!"])


def test_raises_with_exception_value():
with raises(_TestException) as e:
raise _TestException("This is a _TestException")
assert str(e.value) == "This is a _TestException"


def test_raises_with_empty_exception_value():
with raises(_TestException) as e:
raise _TestException
assert str(e.value) == ""


def test_raises_with_none_exception_value(testdir):
testdir.makepyfile(
"""
from pytest_check import raises

def test():
with raises(AssertionError) as e:
x = 1
assert x == 1

assert str(e.value) == "None"
""",
)

result = testdir.runpytest()
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(["FAILURE: None"])
result.stdout.no_fnmatch_line("*assert str(e.value) == 'None'*")
result.stdout.no_fnmatch_line("*AssertionError: assert 'None'*")