From 56537559a34c4bc1e5f11e68e6378a26c241b474 Mon Sep 17 00:00:00 2001 From: Ben Beecher Date: Sun, 22 Jan 2017 17:35:08 -0500 Subject: [PATCH 1/3] Making sure that TestCase can be hashed --- testtools/testcase.py | 7 +++++++ testtools/tests/test_testcase.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/testtools/testcase.py b/testtools/testcase.py index 012def36..2c885452 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -275,6 +275,13 @@ def __eq__(self, other): 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 00cb60da..c2766ffd 100644 --- a/testtools/tests/test_testcase.py +++ b/testtools/tests/test_testcase.py @@ -86,6 +86,10 @@ def test_shortDescription_specified(self): 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") From cf6b8b0ec9e8a16fa64bddbe0ae69f0f235a550a Mon Sep 17 00:00:00 2001 From: Ben Beecher Date: Thu, 29 Aug 2019 15:52:02 -0400 Subject: [PATCH 2/3] less code --- testtools/testcase.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/testtools/testcase.py b/testtools/testcase.py index 2c885452..ea5a8d7f 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -276,11 +276,8 @@ def __eq__(self, other): 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) - + hashfn = getattr(unittest.TestCase, '__hash__', id) + return hashfn(self) def __repr__(self): # We add id to the repr because it makes testing testtools easier. From e223b5dfada74e8a7438f699c81ab2baeb08df60 Mon Sep 17 00:00:00 2001 From: Ben Beecher Date: Sat, 30 Apr 2022 20:05:24 -0400 Subject: [PATCH 3/3] simplifying code --- testtools/testcase.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/testtools/testcase.py b/testtools/testcase.py index ea5a8d7f..f3bd510b 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -275,9 +275,7 @@ def __eq__(self, other): return False return self.__dict__ == other.__dict__ - def __hash__(self): - hashfn = getattr(unittest.TestCase, '__hash__', id) - return hashfn(self) + __hash__ = unittest.TestCase.__hash__ def __repr__(self): # We add id to the repr because it makes testing testtools easier.