summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2009-04-05 19:19:28 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2009-04-05 19:19:28 (GMT)
commitf2dfef1637706e2d103151f1d333b33fc26018b0 (patch)
tree01db417483f401d70c25256cc25f6ffd8f98a96a /Lib
parent7ab5eb91b7aba558ef39d7fa4ca7d3a63d4e886e (diff)
downloadcpython-f2dfef1637706e2d103151f1d333b33fc26018b0.zip
cpython-f2dfef1637706e2d103151f1d333b33fc26018b0.tar.gz
cpython-f2dfef1637706e2d103151f1d333b33fc26018b0.tar.bz2
Adding assertIs and assertIsNot methods to unittest.TestCase
Issue #2578
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_unittest.py23
-rw-r--r--Lib/unittest.py12
2 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py
index 38c4f8f..f28b1af 100644
--- a/Lib/test/test_unittest.py
+++ b/Lib/test/test_unittest.py
@@ -2301,6 +2301,16 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
# from this TestCase instance but since its a local nothing else
# will ever notice that.
+ def testAssertIs(self):
+ thing = object()
+ self.assertIs(thing, thing)
+ self.assertRaises(self.failureException, self.assertIs, thing, object())
+
+ def testAssertIsNot(self):
+ thing = object()
+ self.assertIsNot(thing, object())
+ self.assertRaises(self.failureException, self.assertIsNot, thing, thing)
+
def testAssertIn(self):
animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}
@@ -2444,6 +2454,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
# Test that sequences of unhashable objects can be tested for sameness:
self.assertSameElements([[1, 2], [3, 4]], [[3, 4], [1, 2]])
+
self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])
self.assertRaises(self.failureException, self.assertSameElements,
[[1]], [[2]])
@@ -3016,6 +3027,18 @@ class TestLongMessage(TestCase):
"^unexpectedly None$",
"^unexpectedly None : oops$"])
+ def testAssertIs(self):
+ self.assertMessages('assertIs', (None, 'foo'),
+ ["^None is not 'foo'$", "^oops$",
+ "^None is not 'foo'$",
+ "^None is not 'foo' : oops$"])
+
+ def testAssertIsNot(self):
+ self.assertMessages('assertIsNot', (None, None),
+ ["^unexpectedly identical: None$", "^oops$",
+ "^unexpectedly identical: None$",
+ "^unexpectedly identical: None : oops$"])
+
######################################################################
## Main
diff --git a/Lib/unittest.py b/Lib/unittest.py
index 83790fc..f99f958 100644
--- a/Lib/unittest.py
+++ b/Lib/unittest.py
@@ -806,6 +806,18 @@ class TestCase(object):
standardMsg = '%r unexpectedly found in %r' % (member, container)
self.fail(self._formatMessage(msg, standardMsg))
+ def assertIs(self, expr1, expr2, msg=None):
+ """Just like self.assertTrue(a is b), but with a nicer default message."""
+ if expr1 is not expr2:
+ standardMsg = '%r is not %r' % (expr1, expr2)
+ self.fail(self._formatMessage(msg, standardMsg))
+
+ def assertIsNot(self, expr1, expr2, msg=None):
+ """Just like self.assertTrue(a is not b), but with a nicer default message."""
+ if expr1 is expr2:
+ standardMsg = 'unexpectedly identical: %r' % (expr1,)
+ self.fail(self._formatMessage(msg, standardMsg))
+
def assertDictEqual(self, d1, d2, msg=None):
self.assert_(isinstance(d1, dict), 'First argument is not a dictionary')
self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary')