Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone 2009, All rights reserved |
| 2 | # Copyright (c) 2001-2009 Twisted Matrix Laboratories. |
| 3 | # See LICENSE for details. |
| 4 | |
| 5 | """ |
| 6 | Helpers for the OpenSSL test suite, largely copied from |
| 7 | U{Twisted<http://twistedmatrix.com/>}. |
| 8 | """ |
| 9 | |
| 10 | import shutil |
| 11 | import os, os.path |
| 12 | from tempfile import mktemp |
| 13 | from unittest import TestCase |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame^] | 14 | import sys |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 15 | |
| 16 | |
| 17 | class TestCase(TestCase): |
| 18 | """ |
| 19 | L{TestCase} adds useful testing functionality beyond what is available |
| 20 | from the standard library L{unittest.TestCase}. |
| 21 | """ |
| 22 | def tearDown(self): |
| 23 | """ |
| 24 | Clean up any files or directories created using L{TestCase.mktemp}. |
| 25 | Subclasses must invoke this method if they override it or the |
| 26 | cleanup will not occur. |
| 27 | """ |
| 28 | if self._temporaryFiles is not None: |
| 29 | for temp in self._temporaryFiles: |
| 30 | if os.path.isdir(temp): |
| 31 | shutil.rmtree(temp) |
| 32 | elif os.path.exists(temp): |
| 33 | os.unlink(temp) |
| 34 | |
| 35 | |
| 36 | def failUnlessIdentical(self, first, second, msg=None): |
| 37 | """ |
| 38 | Fail the test if C{first} is not C{second}. This is an |
| 39 | obect-identity-equality test, not an object equality |
| 40 | (i.e. C{__eq__}) test. |
| 41 | |
| 42 | @param msg: if msg is None, then the failure message will be |
| 43 | '%r is not %r' % (first, second) |
| 44 | """ |
| 45 | if first is not second: |
| 46 | raise self.failureException(msg or '%r is not %r' % (first, second)) |
| 47 | return first |
| 48 | assertIdentical = failUnlessIdentical |
| 49 | |
| 50 | |
| 51 | def failIfIdentical(self, first, second, msg=None): |
| 52 | """ |
| 53 | Fail the test if C{first} is C{second}. This is an |
| 54 | obect-identity-equality test, not an object equality |
| 55 | (i.e. C{__eq__}) test. |
| 56 | |
| 57 | @param msg: if msg is None, then the failure message will be |
| 58 | '%r is %r' % (first, second) |
| 59 | """ |
| 60 | if first is second: |
| 61 | raise self.failureException(msg or '%r is %r' % (first, second)) |
| 62 | return first |
| 63 | assertNotIdentical = failIfIdentical |
| 64 | |
| 65 | |
| 66 | def failUnlessRaises(self, exception, f, *args, **kwargs): |
| 67 | """ |
| 68 | Fail the test unless calling the function C{f} with the given |
| 69 | C{args} and C{kwargs} raises C{exception}. The failure will report |
| 70 | the traceback and call stack of the unexpected exception. |
| 71 | |
| 72 | @param exception: exception type that is to be expected |
| 73 | @param f: the function to call |
| 74 | |
| 75 | @return: The raised exception instance, if it is of the given type. |
| 76 | @raise self.failureException: Raised if the function call does |
| 77 | not raise an exception or if it raises an exception of a |
| 78 | different type. |
| 79 | """ |
| 80 | try: |
| 81 | result = f(*args, **kwargs) |
| 82 | except exception, inst: |
| 83 | return inst |
| 84 | except: |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame^] | 85 | raise self.failureException('%s raised instead of %s' |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 86 | % (sys.exc_info()[0], |
| 87 | exception.__name__, |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame^] | 88 | )) |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 89 | else: |
| 90 | raise self.failureException('%s not raised (%r returned)' |
| 91 | % (exception.__name__, result)) |
| 92 | assertRaises = failUnlessRaises |
| 93 | |
| 94 | |
| 95 | _temporaryFiles = None |
| 96 | def mktemp(self): |
| 97 | """ |
| 98 | Pathetic substitute for twisted.trial.unittest.TestCase.mktemp. |
| 99 | """ |
| 100 | if self._temporaryFiles is None: |
| 101 | self._temporaryFiles = [] |
| 102 | temp = mktemp(dir=".") |
| 103 | self._temporaryFiles.append(temp) |
| 104 | return temp |
| 105 | |
| 106 | |
| 107 | # Python 2.3 compatibility. |
| 108 | def assertTrue(self, *a, **kw): |
| 109 | return self.failUnless(*a, **kw) |
| 110 | |
| 111 | |
| 112 | def assertFalse(self, *a, **kw): |
| 113 | return self.failIf(*a, **kw) |