summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Beecher <benbeecher@gmail.com>2017-01-22 17:35:08 -0500
committerJelmer Vernooij <jelmer@jelmer.uk>2022-07-01 19:35:02 +0100
commit56537559a34c4bc1e5f11e68e6378a26c241b474 (patch)
treeeaa530e3d9381fcffd21c0aa6be3918202fe0fc0
parentda597fa5de20687ac45142e90c72382f4b694c05 (diff)
downloadtesttools-56537559a34c4bc1e5f11e68e6378a26c241b474.tar.gz
Making sure that TestCase can be hashed
-rw-r--r--testtools/testcase.py7
-rw-r--r--testtools/tests/test_testcase.py4
2 files changed, 11 insertions, 0 deletions
diff --git a/testtools/testcase.py b/testtools/testcase.py
index 012def3..2c88545 100644
--- a/testtools/testcase.py
+++ b/testtools/testcase.py
@@ -275,6 +275,13 @@ class TestCase(unittest.TestCase):
return False
return self.__dict__ == other.__dict__
+ def __hash__(self):
+ hashfn = getattr(unittest.TestCase, '__hash__', None)
+ if hashfn is not None:
+ return hashfn(self)
+ return id(self)
+
+
def __repr__(self):
# We add id to the repr because it makes testing testtools easier.
return f"<{self.id()} id=0x{id(self):0x}>"
diff --git a/testtools/tests/test_testcase.py b/testtools/tests/test_testcase.py
index 00cb60d..c2766ff 100644
--- a/testtools/tests/test_testcase.py
+++ b/testtools/tests/test_testcase.py
@@ -86,6 +86,10 @@ class TestPlaceHolder(TestCase):
test = PlaceHolder("test id", "description")
self.assertEqual("description", test.shortDescription())
+ def test_testcase_is_hashable(self):
+ test = hash(self)
+ self.assertEqual(unittest.TestCase.__hash__(self), test)
+
def test_repr_just_id(self):
# repr(placeholder) shows you how the object was constructed.
test = PlaceHolder("test id")