Module unittestzero
[hide private]
[frames] | no frames]

Source Code for Module unittestzero

1 #!/usr/bin/env python 2 # ***** BEGIN LICENSE BLOCK ***** 3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1 4 # 5 # The contents of this file are subject to the Mozilla Public License Version 6 # 1.1 (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # http://www.mozilla.org/MPL/ 9 # 10 # Software distributed under the License is distributed on an "AS IS" basis, 11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 12 # for the specific language governing rights and limitations under the 13 # License. 14 # 15 # The Original Code is UnittestZero. 16 # 17 # The Initial Developer of the Original Code is 18 # Portions created by the Initial Developer are Copyright (C) 2011 19 20 # the Initial Developer. All Rights Reserved. 21 # 22 # Contributor(s): David Burns 23 # Joel Andersson <janderssn@gmail.com> 24 # Bebe<florin.strugariu@softvision.ro> 25 # 26 # Alternatively, the contents of this file may be used under the terms of 27 # either the GNU General Public License Version 2 or later (the "GPL"), or 28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 29 # in which case the provisions of the GPL or the LGPL are applicable instead 30 # of those above. If you wish to allow use of your version of this file only 31 # under the terms of either the GPL or the LGPL, and not to allow others to 32 # use your version of this file under the terms of the MPL, indicate your 33 # decision by deleting the provisions above and replace them with the notice 34 # and other provisions required by the GPL or the LGPL. If you do not delete 35 # the provisions above, a recipient may use your version of this file under 36 # the terms of any one of the MPL, the GPL or the LGPL. 37 # 38 # ***** END LICENSE BLOCK ***** 39 40 41 -class Assert:
42 43 @classmethod
44 - def equal(self, first, second, msg=None):
45 """ 46 Asserts that 2 elements are the same 47 48 :Args: 49 - First object to be tested 50 - Second object to be tested 51 - Message that will be printed if it fails 52 """ 53 assert first == second, msg
54 55 @classmethod
56 - def not_equal(self, first, second, msg=None):
57 """ 58 Asserts that 2 elements are the same 59 60 :Args: 61 - First object to be tested 62 - Second object to be tested 63 - Message that will be printed if it fails 64 """ 65 assert first != second, msg
66 67 @classmethod
68 - def true(self, first, msg=None):
69 """ 70 Asserts that what is given is equal to True 71 72 :Args: 73 - First object to be tested 74 - Message that will be printed if it fails 75 """ 76 77 assert first is True, msg
78 79 @classmethod
80 - def false(self, first, msg=None):
81 """ 82 Asserts that what is given is equal to False 83 84 :Args: 85 - First object to be tested 86 - Message that will be printed if it fails 87 """ 88 89 assert first is False, msg
90 91 @classmethod
92 - def none(self, first, msg=None):
93 """ 94 Asserts that what is given is equal to None 95 96 :Args: 97 - First object to be tested 98 - Message that will be printed if it fails 99 """ 100 101 assert first is None, msg
102 103 @classmethod
104 - def not_none(self, first, msg=None):
105 """ 106 Asserts that what is given is not equal to None 107 108 :Args: 109 - First object to be tested 110 - Message that will be printed if it fails 111 """ 112 113 assert first is not None, msg
114 115 @classmethod
116 - def fail(self, msg):
117 """ 118 Raises an assertion error with a message passed in 119 120 :Args: 121 - Message that will be printed 122 """ 123 raise AssertionError(msg)
124 125 @classmethod
126 - def is_sorted_ascending(self, iterable, msg=''):
127 """ 128 Goes through a list and asserts that items in the list are sorted ascendingly 129 130 :Args: 131 - List that will be asserted against 132 - Message that will be printed if it fails 133 """ 134 for i in xrange(len(iterable) - 1): 135 assert iterable[i] <= iterable[i + 1], '. '.join(['%s is not before %s' % (iterable[i + 1], iterable[i]), msg])
136 137 @classmethod
138 - def is_sorted_descending(self, iterable, msg=''):
139 """ 140 Goes through a list and asserts that items in the list are sorted descendingly 141 142 :Args: 143 - List that will be asserted against 144 - Message that will be printed if it fails 145 """ 146 147 for i in xrange(len(iterable) - 1): 148 assert iterable[i] >= iterable[i + 1], '. '.join(['%s is not before %s' % (iterable[i], iterable[i + 1]), msg])
149 150 @classmethod
151 - def raises(self, exception, caller, msg=None, *args, **kwargs):
152 """ 153 Asserts that an Error is raised when calling a method 154 155 :Args: 156 - Error class 157 - method to be called 158 - Message that will be printed if it fails 159 - args that will be passed to the caller 160 - kwargs that will be passed to the caller 161 """ 162 try: 163 caller(*args, **kwargs) 164 except exception: 165 return 166 167 if hasattr(exception, '__name__'): 168 excName = exception.__name__ 169 else: 170 excName = str(exception) 171 172 raise AssertionError("%s was not raised" % excName)
173 174 @classmethod
175 - def contains(self, needle, haystack):
176 try: 177 assert needle in haystack 178 except AssertionError: 179 raise AssertionError('%s is not found in %s' % (needle, haystack))
180 181 @classmethod
182 - def less(self, first, second, msg=None):
183 """ 184 Asserts that first element is < the second element 185 186 :Args: 187 - First object to be tested 188 - Second object to be tested 189 - Message that will be printed if it fails 190 """ 191 assert first < second, msg
192 193 @classmethod
194 - def greater(self, first, second, msg=None):
195 """ 196 Asserts that first element is greater than the second element 197 198 :Args: 199 - First object to be tested 200 - Second object to be tested 201 - Message that will be printed if it fails 202 """ 203 204 assert first > second, msg
205 206 @classmethod
207 - def less_equal(self, first, second, msg=None):
208 """ 209 Asserts that the first element is <= to the second element 210 211 :Args: 212 - First object to be tested 213 - Second object to be tested 214 - Message that will be printed if it fails 215 """ 216 217 assert first <= second, msg
218 219 @classmethod
220 - def greater_equal(self, first, second, msg=None):
221 """ 222 Asserts that first element is >= to the send element 223 224 :Args: 225 - First object to be tested 226 - Second object to be tested 227 - Message that will be printed if it fails 228 """ 229 230 assert first >= second, msg
231