aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Doyle <chief@google.com>2022-08-19 11:16:36 -0700
committerCopybara-Service <copybara-worker@google.com>2022-08-19 11:17:00 -0700
commit2e3d4b4de7e576bba59d247a956c6e5fe3c0c5bb (patch)
treefa48db3efbe0344f3bf741a61c958bd196cabce8
parent3e411327e50766b3108674f637fd8ad895fb6e62 (diff)
downloadabsl-py-2e3d4b4de7e576bba59d247a956c6e5fe3c0c5bb.tar.gz
Updates the absl `assertRaisesWithPredicateMatch` and `assertRaisesWithLiteralMatch` method to also store the caught exception object in the returned context manager's `exception` attribute.
PiperOrigin-RevId: 468744160 Change-Id: Iebc6e20d2084d1d078f5dd989d705209a94e1e45
-rw-r--r--CHANGELOG.md10
-rw-r--r--absl/testing/absltest.py2
-rw-r--r--absl/testing/tests/absltest_test.py23
3 files changed, 32 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ab288ff..5464857 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,7 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com).
## Unreleased
-Nothing notable unreleased.
+### Changed
+
+* (testing) Assertions `assertRaisesWithPredicateMatch` and
+ `assertRaisesWithLiteralMatch` now capture the raised `Exception` for
+ further analysis when used as a context manager.
## 1.2.0 (2022-07-18)
@@ -241,8 +245,8 @@ Nothing notable unreleased.
* (flags) An empty --flagfile value (e.g. "--flagfile=" or "--flagfile=''"
doesn't raise an error; its not just ignored. This matches Abseil C++
behavior.
-* (bazel) Building with Bazel 0.2.0 works without extra incompatibility disable
- build flags.
+* (bazel) Building with Bazel 0.2.0 works without extra incompatibility
+ disable build flags.
### Changed
diff --git a/absl/testing/absltest.py b/absl/testing/absltest.py
index 0620504..d1b0e59 100644
--- a/absl/testing/absltest.py
+++ b/absl/testing/absltest.py
@@ -1336,6 +1336,8 @@ class TestCase(unittest.TestCase):
if not issubclass(exc_type, self.expected_exception):
return False
self.test_func(exc_value)
+ if exc_value:
+ self.exception = exc_value.with_traceback(None)
return True
@typing.overload
diff --git a/absl/testing/tests/absltest_test.py b/absl/testing/tests/absltest_test.py
index 43201bd..68067cf 100644
--- a/absl/testing/tests/absltest_test.py
+++ b/absl/testing/tests/absltest_test.py
@@ -975,6 +975,29 @@ test case
with self.assertRaisesWithPredicateMatch(ValueError, lambda e: True):
raise ValueError
+ def test_assert_raises_with_predicate_match_exception_captured(self):
+ def _raise_value_error():
+ raise ValueError
+
+ predicate = lambda e: e is not None
+ with self.assertRaisesWithPredicateMatch(ValueError, predicate) as ctx_mgr:
+ _raise_value_error()
+
+ expected = getattr(ctx_mgr, 'exception', None)
+ self.assertIsInstance(expected, ValueError)
+
+ def test_assert_raises_with_literal_match_exception_captured(self):
+ message = 'some value error'
+ def _raise_value_error():
+ raise ValueError(message)
+
+ # predicate = lambda e: e is not None
+ with self.assertRaisesWithLiteralMatch(ValueError, message) as ctx_mgr:
+ _raise_value_error()
+
+ expected = getattr(ctx_mgr, 'exception', None)
+ self.assertIsInstance(expected, ValueError)
+
def test_assert_contains_in_order(self):
# Valids
self.assertContainsInOrder(