diff options
-rw-r--r-- | test/backunittest.py | 9 | ||||
-rw-r--r-- | test/test_testing.py | 8 |
2 files changed, 16 insertions, 1 deletions
diff --git a/test/backunittest.py b/test/backunittest.py index 7ec34f5c..84a7f36e 100644 --- a/test/backunittest.py +++ b/test/backunittest.py @@ -29,6 +29,13 @@ class TestCase(unittest.TestCase): if exp: self.fail(msg) + if _need('assertIn'): + def assertIn(self, member, container, msg=None): + """Assert that `member` is in `container`.""" + if member not in container: + msg = msg or ('%r not found in %r' % (member, container)) + self.fail(msg) + if _need('assertRaisesRegexp'): def assertRaisesRegexp(self, excClass, regexp, callobj, *args, **kw): """ Just like unittest.TestCase.assertRaises, @@ -46,7 +53,7 @@ class TestCase(unittest.TestCase): # Message provided, and it didn't match: fail! raise self.failureException( "Right exception, wrong message: " - "'%s' doesn't match '%s'" % (excMsg, regexp) + "%r doesn't match %r" % (excMsg, regexp) ) # No need to catch other exceptions: They'll fail the test all by # themselves! diff --git a/test/test_testing.py b/test/test_testing.py index 316dbc1b..c632f3b5 100644 --- a/test/test_testing.py +++ b/test/test_testing.py @@ -92,6 +92,14 @@ class TestingTest(TestCase): self.assertFalse(False) self.assertRaises(AssertionError, self.assertFalse, True) + def test_assert_contains(self): + self.assertIn("abc", "hello abc") + self.assertIn("abc", ["xyz", "abc", "foo"]) + self.assertIn("abc", {'abc': 1, 'xyz': 2}) + self.assertRaises(AssertionError, self.assertIn, "abc", "xyz") + self.assertRaises(AssertionError, self.assertIn, "abc", ["x", "xabc"]) + self.assertRaises(AssertionError, self.assertIn, "abc", {'x':'abc'}) + class CoverageTestTest(CoverageTest): """Test the methods in `CoverageTest`.""" |