diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2012-11-09 07:19:56 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2012-11-09 07:19:56 -0500 |
commit | 429868d515cc4ea74d625715512c57d3b5b93e73 (patch) | |
tree | 539e1e71c681d7721a0441e3108847129cb5a1f4 | |
parent | c260721a7bbe8d75368fc35daa719655217a83f2 (diff) | |
download | python-coveragepy-git-429868d515cc4ea74d625715512c57d3b5b93e73.tar.gz |
Add assertIn for test use.
-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`.""" |