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