Merge lp:~jelmer/testtools/assertIsInstance-msg into lp:~testtools-committers/testtools/trunk

Proposed by Jelmer Vernooij
Status: Merged
Merged at revision: 158
Proposed branch: lp:~jelmer/testtools/assertIsInstance-msg
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 40 lines (+15/-4)
2 files modified
testtools/testcase.py (+5/-4)
testtools/tests/test_testtools.py (+10/-0)
To merge this branch: bzr merge lp:~jelmer/testtools/assertIsInstance-msg
Reviewer Review Type Date Requested Status
Robert Collins Approve
Review via email: mp+43458@code.launchpad.net

Description of the change

Add an optional "msg" argument for overriding the message to TestCase.assertIsInstance, for compatibility with Python 2.7's TestCase.assertDictEqual.

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

Please add changes like this to NEWS. Also I've simplified your test.

review: Approve
Revision history for this message
Robert Collins (lifeless) wrote :

(And ideally in MANUAL, but jml has that in flux just now).

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'testtools/testcase.py'
2--- testtools/testcase.py 2010-11-27 10:51:14 +0000
3+++ testtools/testcase.py 2010-12-12 04:13:52 +0000
4@@ -300,10 +300,11 @@
5 self.assertTrue(
6 needle not in haystack, '%r in %r' % (needle, haystack))
7
8- def assertIsInstance(self, obj, klass):
9- self.assertTrue(
10- isinstance(obj, klass),
11- '%r is not an instance of %s' % (obj, self._formatTypes(klass)))
12+ def assertIsInstance(self, obj, klass, msg=None):
13+ if msg is None:
14+ msg = '%r is not an instance of %s' % (
15+ obj, self._formatTypes(klass))
16+ self.assertTrue(isinstance(obj, klass), msg)
17
18 def assertRaises(self, excClass, callableObj, *args, **kwargs):
19 """Fail unless an exception of class excClass is thrown
20
21=== modified file 'testtools/tests/test_testtools.py'
22--- testtools/tests/test_testtools.py 2010-11-11 09:46:18 +0000
23+++ testtools/tests/test_testtools.py 2010-12-12 04:13:52 +0000
24@@ -375,6 +375,16 @@
25 '42 is not an instance of %s' % self._formatTypes([Foo, Bar]),
26 self.assertIsInstance, 42, (Foo, Bar))
27
28+ def test_assertIsInstance_overridden_message(self):
29+ # assertIsInstance(obj, klass, msg) fails the test with the specified
30+ # message when obj is not an instance of klass.
31+
32+ class Foo(object):
33+ """Simple class for testing assertIsInstance."""
34+
35+ self.assertFails("Bericht",
36+ self.assertIsInstance, 42, Foo, "Bericht")
37+
38 def test_assertIs(self):
39 # assertIs asserts that an object is identical to another object.
40 self.assertIs(None, None)

Subscribers

People subscribed via source and target branches