From: Brian Warner Date: Wed, 6 Aug 2008 19:05:52 +0000 (-0700) Subject: test/common: add ShouldFailMixin X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/flags/index.php?a=commitdiff_plain;h=a68c9364b8f2392ce4dd36711a38bbae439e227f;p=tahoe-lafs%2Ftahoe-lafs.git test/common: add ShouldFailMixin --- diff --git a/src/allmydata/test/common.py b/src/allmydata/test/common.py index 8866390c..be2234cd 100644 --- a/src/allmydata/test/common.py +++ b/src/allmydata/test/common.py @@ -790,3 +790,43 @@ class ShareManglingMixin(SystemTestMixin): newdata = newshares.get((i, sharenum)) if newdata is not None: open(pathtosharefile, "w").write(newdata) + +class ShouldFailMixin: + def shouldFail(self, expected_failure, which, substring, + callable, *args, **kwargs): + """Assert that a function call raises some exception. This is a + Deferred-friendly version of TestCase.assertRaises() . + + Suppose you want to verify the following function: + + def broken(a, b, c): + if a < 0: + raise TypeError('a must not be negative') + return defer.succeed(b+c) + + You can use: + d = self.shouldFail(TypeError, 'test name', + 'a must not be negative', + broken, -4, 5, c=12) + in your test method. The 'test name' string will be included in the + error message, if any, because Deferred chains frequently make it + difficult to tell which assertion was tripped. + + The substring= argument, if not None, must appear inside the + stringified Failure, or the test will fail. + """ + + assert substring is None or isinstance(substring, str) + d = defer.maybeDeferred(callable, *args, **kwargs) + def done(res): + if isinstance(res, failure.Failure): + res.trap(expected_failure) + if substring: + self.failUnless(substring in str(res), + "substring '%s' not in '%s'" + % (substring, str(res))) + else: + self.fail("%s was supposed to raise %s, not get '%s'" % + (which, expected_failure, res)) + d.addBoth(done) + return d