blob: 72c9e443994c9189c1be37ec8e1030911efa7532 [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
Rick Dean47262da2009-07-08 16:17:17 -050014import sys
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040015
16
17class 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 Dean47262da2009-07-08 16:17:17 -050085 raise self.failureException('%s raised instead of %s'
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040086 % (sys.exc_info()[0],
87 exception.__name__,
Rick Dean47262da2009-07-08 16:17:17 -050088 ))
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040089 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)