Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3..../unittest
File: case.py
raise self.failureException(msg)
[1000] Fix | Delete
[1001] Fix | Delete
def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
[1002] Fix | Delete
"""An equality assertion for ordered sequences (like lists and tuples).
[1003] Fix | Delete
[1004] Fix | Delete
For the purposes of this function, a valid ordered sequence type is one
[1005] Fix | Delete
which can be indexed, has a length, and has an equality operator.
[1006] Fix | Delete
[1007] Fix | Delete
Args:
[1008] Fix | Delete
seq1: The first sequence to compare.
[1009] Fix | Delete
seq2: The second sequence to compare.
[1010] Fix | Delete
seq_type: The expected datatype of the sequences, or None if no
[1011] Fix | Delete
datatype should be enforced.
[1012] Fix | Delete
msg: Optional message to use on failure instead of a list of
[1013] Fix | Delete
differences.
[1014] Fix | Delete
"""
[1015] Fix | Delete
if seq_type is not None:
[1016] Fix | Delete
seq_type_name = seq_type.__name__
[1017] Fix | Delete
if not isinstance(seq1, seq_type):
[1018] Fix | Delete
raise self.failureException('First sequence is not a %s: %s'
[1019] Fix | Delete
% (seq_type_name, safe_repr(seq1)))
[1020] Fix | Delete
if not isinstance(seq2, seq_type):
[1021] Fix | Delete
raise self.failureException('Second sequence is not a %s: %s'
[1022] Fix | Delete
% (seq_type_name, safe_repr(seq2)))
[1023] Fix | Delete
else:
[1024] Fix | Delete
seq_type_name = "sequence"
[1025] Fix | Delete
[1026] Fix | Delete
differing = None
[1027] Fix | Delete
try:
[1028] Fix | Delete
len1 = len(seq1)
[1029] Fix | Delete
except (TypeError, NotImplementedError):
[1030] Fix | Delete
differing = 'First %s has no length. Non-sequence?' % (
[1031] Fix | Delete
seq_type_name)
[1032] Fix | Delete
[1033] Fix | Delete
if differing is None:
[1034] Fix | Delete
try:
[1035] Fix | Delete
len2 = len(seq2)
[1036] Fix | Delete
except (TypeError, NotImplementedError):
[1037] Fix | Delete
differing = 'Second %s has no length. Non-sequence?' % (
[1038] Fix | Delete
seq_type_name)
[1039] Fix | Delete
[1040] Fix | Delete
if differing is None:
[1041] Fix | Delete
if seq1 == seq2:
[1042] Fix | Delete
return
[1043] Fix | Delete
[1044] Fix | Delete
differing = '%ss differ: %s != %s\n' % (
[1045] Fix | Delete
(seq_type_name.capitalize(),) +
[1046] Fix | Delete
_common_shorten_repr(seq1, seq2))
[1047] Fix | Delete
[1048] Fix | Delete
for i in range(min(len1, len2)):
[1049] Fix | Delete
try:
[1050] Fix | Delete
item1 = seq1[i]
[1051] Fix | Delete
except (TypeError, IndexError, NotImplementedError):
[1052] Fix | Delete
differing += ('\nUnable to index element %d of first %s\n' %
[1053] Fix | Delete
(i, seq_type_name))
[1054] Fix | Delete
break
[1055] Fix | Delete
[1056] Fix | Delete
try:
[1057] Fix | Delete
item2 = seq2[i]
[1058] Fix | Delete
except (TypeError, IndexError, NotImplementedError):
[1059] Fix | Delete
differing += ('\nUnable to index element %d of second %s\n' %
[1060] Fix | Delete
(i, seq_type_name))
[1061] Fix | Delete
break
[1062] Fix | Delete
[1063] Fix | Delete
if item1 != item2:
[1064] Fix | Delete
differing += ('\nFirst differing element %d:\n%s\n%s\n' %
[1065] Fix | Delete
((i,) + _common_shorten_repr(item1, item2)))
[1066] Fix | Delete
break
[1067] Fix | Delete
else:
[1068] Fix | Delete
if (len1 == len2 and seq_type is None and
[1069] Fix | Delete
type(seq1) != type(seq2)):
[1070] Fix | Delete
# The sequences are the same, but have differing types.
[1071] Fix | Delete
return
[1072] Fix | Delete
[1073] Fix | Delete
if len1 > len2:
[1074] Fix | Delete
differing += ('\nFirst %s contains %d additional '
[1075] Fix | Delete
'elements.\n' % (seq_type_name, len1 - len2))
[1076] Fix | Delete
try:
[1077] Fix | Delete
differing += ('First extra element %d:\n%s\n' %
[1078] Fix | Delete
(len2, safe_repr(seq1[len2])))
[1079] Fix | Delete
except (TypeError, IndexError, NotImplementedError):
[1080] Fix | Delete
differing += ('Unable to index element %d '
[1081] Fix | Delete
'of first %s\n' % (len2, seq_type_name))
[1082] Fix | Delete
elif len1 < len2:
[1083] Fix | Delete
differing += ('\nSecond %s contains %d additional '
[1084] Fix | Delete
'elements.\n' % (seq_type_name, len2 - len1))
[1085] Fix | Delete
try:
[1086] Fix | Delete
differing += ('First extra element %d:\n%s\n' %
[1087] Fix | Delete
(len1, safe_repr(seq2[len1])))
[1088] Fix | Delete
except (TypeError, IndexError, NotImplementedError):
[1089] Fix | Delete
differing += ('Unable to index element %d '
[1090] Fix | Delete
'of second %s\n' % (len1, seq_type_name))
[1091] Fix | Delete
standardMsg = differing
[1092] Fix | Delete
diffMsg = '\n' + '\n'.join(
[1093] Fix | Delete
difflib.ndiff(pprint.pformat(seq1).splitlines(),
[1094] Fix | Delete
pprint.pformat(seq2).splitlines()))
[1095] Fix | Delete
[1096] Fix | Delete
standardMsg = self._truncateMessage(standardMsg, diffMsg)
[1097] Fix | Delete
msg = self._formatMessage(msg, standardMsg)
[1098] Fix | Delete
self.fail(msg)
[1099] Fix | Delete
[1100] Fix | Delete
def _truncateMessage(self, message, diff):
[1101] Fix | Delete
max_diff = self.maxDiff
[1102] Fix | Delete
if max_diff is None or len(diff) <= max_diff:
[1103] Fix | Delete
return message + diff
[1104] Fix | Delete
return message + (DIFF_OMITTED % len(diff))
[1105] Fix | Delete
[1106] Fix | Delete
def assertListEqual(self, list1, list2, msg=None):
[1107] Fix | Delete
"""A list-specific equality assertion.
[1108] Fix | Delete
[1109] Fix | Delete
Args:
[1110] Fix | Delete
list1: The first list to compare.
[1111] Fix | Delete
list2: The second list to compare.
[1112] Fix | Delete
msg: Optional message to use on failure instead of a list of
[1113] Fix | Delete
differences.
[1114] Fix | Delete
[1115] Fix | Delete
"""
[1116] Fix | Delete
self.assertSequenceEqual(list1, list2, msg, seq_type=list)
[1117] Fix | Delete
[1118] Fix | Delete
def assertTupleEqual(self, tuple1, tuple2, msg=None):
[1119] Fix | Delete
"""A tuple-specific equality assertion.
[1120] Fix | Delete
[1121] Fix | Delete
Args:
[1122] Fix | Delete
tuple1: The first tuple to compare.
[1123] Fix | Delete
tuple2: The second tuple to compare.
[1124] Fix | Delete
msg: Optional message to use on failure instead of a list of
[1125] Fix | Delete
differences.
[1126] Fix | Delete
"""
[1127] Fix | Delete
self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)
[1128] Fix | Delete
[1129] Fix | Delete
def assertSetEqual(self, set1, set2, msg=None):
[1130] Fix | Delete
"""A set-specific equality assertion.
[1131] Fix | Delete
[1132] Fix | Delete
Args:
[1133] Fix | Delete
set1: The first set to compare.
[1134] Fix | Delete
set2: The second set to compare.
[1135] Fix | Delete
msg: Optional message to use on failure instead of a list of
[1136] Fix | Delete
differences.
[1137] Fix | Delete
[1138] Fix | Delete
assertSetEqual uses ducktyping to support different types of sets, and
[1139] Fix | Delete
is optimized for sets specifically (parameters must support a
[1140] Fix | Delete
difference method).
[1141] Fix | Delete
"""
[1142] Fix | Delete
try:
[1143] Fix | Delete
difference1 = set1.difference(set2)
[1144] Fix | Delete
except TypeError as e:
[1145] Fix | Delete
self.fail('invalid type when attempting set difference: %s' % e)
[1146] Fix | Delete
except AttributeError as e:
[1147] Fix | Delete
self.fail('first argument does not support set difference: %s' % e)
[1148] Fix | Delete
[1149] Fix | Delete
try:
[1150] Fix | Delete
difference2 = set2.difference(set1)
[1151] Fix | Delete
except TypeError as e:
[1152] Fix | Delete
self.fail('invalid type when attempting set difference: %s' % e)
[1153] Fix | Delete
except AttributeError as e:
[1154] Fix | Delete
self.fail('second argument does not support set difference: %s' % e)
[1155] Fix | Delete
[1156] Fix | Delete
if not (difference1 or difference2):
[1157] Fix | Delete
return
[1158] Fix | Delete
[1159] Fix | Delete
lines = []
[1160] Fix | Delete
if difference1:
[1161] Fix | Delete
lines.append('Items in the first set but not the second:')
[1162] Fix | Delete
for item in difference1:
[1163] Fix | Delete
lines.append(repr(item))
[1164] Fix | Delete
if difference2:
[1165] Fix | Delete
lines.append('Items in the second set but not the first:')
[1166] Fix | Delete
for item in difference2:
[1167] Fix | Delete
lines.append(repr(item))
[1168] Fix | Delete
[1169] Fix | Delete
standardMsg = '\n'.join(lines)
[1170] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1171] Fix | Delete
[1172] Fix | Delete
def assertIn(self, member, container, msg=None):
[1173] Fix | Delete
"""Just like self.assertTrue(a in b), but with a nicer default message."""
[1174] Fix | Delete
if member not in container:
[1175] Fix | Delete
standardMsg = '%s not found in %s' % (safe_repr(member),
[1176] Fix | Delete
safe_repr(container))
[1177] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1178] Fix | Delete
[1179] Fix | Delete
def assertNotIn(self, member, container, msg=None):
[1180] Fix | Delete
"""Just like self.assertTrue(a not in b), but with a nicer default message."""
[1181] Fix | Delete
if member in container:
[1182] Fix | Delete
standardMsg = '%s unexpectedly found in %s' % (safe_repr(member),
[1183] Fix | Delete
safe_repr(container))
[1184] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1185] Fix | Delete
[1186] Fix | Delete
def assertIs(self, expr1, expr2, msg=None):
[1187] Fix | Delete
"""Just like self.assertTrue(a is b), but with a nicer default message."""
[1188] Fix | Delete
if expr1 is not expr2:
[1189] Fix | Delete
standardMsg = '%s is not %s' % (safe_repr(expr1),
[1190] Fix | Delete
safe_repr(expr2))
[1191] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1192] Fix | Delete
[1193] Fix | Delete
def assertIsNot(self, expr1, expr2, msg=None):
[1194] Fix | Delete
"""Just like self.assertTrue(a is not b), but with a nicer default message."""
[1195] Fix | Delete
if expr1 is expr2:
[1196] Fix | Delete
standardMsg = 'unexpectedly identical: %s' % (safe_repr(expr1),)
[1197] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1198] Fix | Delete
[1199] Fix | Delete
def assertDictEqual(self, d1, d2, msg=None):
[1200] Fix | Delete
self.assertIsInstance(d1, dict, 'First argument is not a dictionary')
[1201] Fix | Delete
self.assertIsInstance(d2, dict, 'Second argument is not a dictionary')
[1202] Fix | Delete
[1203] Fix | Delete
if d1 != d2:
[1204] Fix | Delete
standardMsg = '%s != %s' % _common_shorten_repr(d1, d2)
[1205] Fix | Delete
diff = ('\n' + '\n'.join(difflib.ndiff(
[1206] Fix | Delete
pprint.pformat(d1).splitlines(),
[1207] Fix | Delete
pprint.pformat(d2).splitlines())))
[1208] Fix | Delete
standardMsg = self._truncateMessage(standardMsg, diff)
[1209] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1210] Fix | Delete
[1211] Fix | Delete
def assertDictContainsSubset(self, subset, dictionary, msg=None):
[1212] Fix | Delete
"""Checks whether dictionary is a superset of subset."""
[1213] Fix | Delete
warnings.warn('assertDictContainsSubset is deprecated',
[1214] Fix | Delete
DeprecationWarning)
[1215] Fix | Delete
missing = []
[1216] Fix | Delete
mismatched = []
[1217] Fix | Delete
for key, value in subset.items():
[1218] Fix | Delete
if key not in dictionary:
[1219] Fix | Delete
missing.append(key)
[1220] Fix | Delete
elif value != dictionary[key]:
[1221] Fix | Delete
mismatched.append('%s, expected: %s, actual: %s' %
[1222] Fix | Delete
(safe_repr(key), safe_repr(value),
[1223] Fix | Delete
safe_repr(dictionary[key])))
[1224] Fix | Delete
[1225] Fix | Delete
if not (missing or mismatched):
[1226] Fix | Delete
return
[1227] Fix | Delete
[1228] Fix | Delete
standardMsg = ''
[1229] Fix | Delete
if missing:
[1230] Fix | Delete
standardMsg = 'Missing: %s' % ','.join(safe_repr(m) for m in
[1231] Fix | Delete
missing)
[1232] Fix | Delete
if mismatched:
[1233] Fix | Delete
if standardMsg:
[1234] Fix | Delete
standardMsg += '; '
[1235] Fix | Delete
standardMsg += 'Mismatched values: %s' % ','.join(mismatched)
[1236] Fix | Delete
[1237] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1238] Fix | Delete
[1239] Fix | Delete
[1240] Fix | Delete
def assertCountEqual(self, first, second, msg=None):
[1241] Fix | Delete
"""Asserts that two iterables have the same elements, the same number of
[1242] Fix | Delete
times, without regard to order.
[1243] Fix | Delete
[1244] Fix | Delete
self.assertEqual(Counter(list(first)),
[1245] Fix | Delete
Counter(list(second)))
[1246] Fix | Delete
[1247] Fix | Delete
Example:
[1248] Fix | Delete
- [0, 1, 1] and [1, 0, 1] compare equal.
[1249] Fix | Delete
- [0, 0, 1] and [0, 1] compare unequal.
[1250] Fix | Delete
[1251] Fix | Delete
"""
[1252] Fix | Delete
first_seq, second_seq = list(first), list(second)
[1253] Fix | Delete
try:
[1254] Fix | Delete
first = collections.Counter(first_seq)
[1255] Fix | Delete
second = collections.Counter(second_seq)
[1256] Fix | Delete
except TypeError:
[1257] Fix | Delete
# Handle case with unhashable elements
[1258] Fix | Delete
differences = _count_diff_all_purpose(first_seq, second_seq)
[1259] Fix | Delete
else:
[1260] Fix | Delete
if first == second:
[1261] Fix | Delete
return
[1262] Fix | Delete
differences = _count_diff_hashable(first_seq, second_seq)
[1263] Fix | Delete
[1264] Fix | Delete
if differences:
[1265] Fix | Delete
standardMsg = 'Element counts were not equal:\n'
[1266] Fix | Delete
lines = ['First has %d, Second has %d: %r' % diff for diff in differences]
[1267] Fix | Delete
diffMsg = '\n'.join(lines)
[1268] Fix | Delete
standardMsg = self._truncateMessage(standardMsg, diffMsg)
[1269] Fix | Delete
msg = self._formatMessage(msg, standardMsg)
[1270] Fix | Delete
self.fail(msg)
[1271] Fix | Delete
[1272] Fix | Delete
def assertMultiLineEqual(self, first, second, msg=None):
[1273] Fix | Delete
"""Assert that two multi-line strings are equal."""
[1274] Fix | Delete
self.assertIsInstance(first, str, 'First argument is not a string')
[1275] Fix | Delete
self.assertIsInstance(second, str, 'Second argument is not a string')
[1276] Fix | Delete
[1277] Fix | Delete
if first != second:
[1278] Fix | Delete
# don't use difflib if the strings are too long
[1279] Fix | Delete
if (len(first) > self._diffThreshold or
[1280] Fix | Delete
len(second) > self._diffThreshold):
[1281] Fix | Delete
self._baseAssertEqual(first, second, msg)
[1282] Fix | Delete
firstlines = first.splitlines(keepends=True)
[1283] Fix | Delete
secondlines = second.splitlines(keepends=True)
[1284] Fix | Delete
if len(firstlines) == 1 and first.strip('\r\n') == first:
[1285] Fix | Delete
firstlines = [first + '\n']
[1286] Fix | Delete
secondlines = [second + '\n']
[1287] Fix | Delete
standardMsg = '%s != %s' % _common_shorten_repr(first, second)
[1288] Fix | Delete
diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
[1289] Fix | Delete
standardMsg = self._truncateMessage(standardMsg, diff)
[1290] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1291] Fix | Delete
[1292] Fix | Delete
def assertLess(self, a, b, msg=None):
[1293] Fix | Delete
"""Just like self.assertTrue(a < b), but with a nicer default message."""
[1294] Fix | Delete
if not a < b:
[1295] Fix | Delete
standardMsg = '%s not less than %s' % (safe_repr(a), safe_repr(b))
[1296] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1297] Fix | Delete
[1298] Fix | Delete
def assertLessEqual(self, a, b, msg=None):
[1299] Fix | Delete
"""Just like self.assertTrue(a <= b), but with a nicer default message."""
[1300] Fix | Delete
if not a <= b:
[1301] Fix | Delete
standardMsg = '%s not less than or equal to %s' % (safe_repr(a), safe_repr(b))
[1302] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1303] Fix | Delete
[1304] Fix | Delete
def assertGreater(self, a, b, msg=None):
[1305] Fix | Delete
"""Just like self.assertTrue(a > b), but with a nicer default message."""
[1306] Fix | Delete
if not a > b:
[1307] Fix | Delete
standardMsg = '%s not greater than %s' % (safe_repr(a), safe_repr(b))
[1308] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1309] Fix | Delete
[1310] Fix | Delete
def assertGreaterEqual(self, a, b, msg=None):
[1311] Fix | Delete
"""Just like self.assertTrue(a >= b), but with a nicer default message."""
[1312] Fix | Delete
if not a >= b:
[1313] Fix | Delete
standardMsg = '%s not greater than or equal to %s' % (safe_repr(a), safe_repr(b))
[1314] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1315] Fix | Delete
[1316] Fix | Delete
def assertIsNone(self, obj, msg=None):
[1317] Fix | Delete
"""Same as self.assertTrue(obj is None), with a nicer default message."""
[1318] Fix | Delete
if obj is not None:
[1319] Fix | Delete
standardMsg = '%s is not None' % (safe_repr(obj),)
[1320] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1321] Fix | Delete
[1322] Fix | Delete
def assertIsNotNone(self, obj, msg=None):
[1323] Fix | Delete
"""Included for symmetry with assertIsNone."""
[1324] Fix | Delete
if obj is None:
[1325] Fix | Delete
standardMsg = 'unexpectedly None'
[1326] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1327] Fix | Delete
[1328] Fix | Delete
def assertIsInstance(self, obj, cls, msg=None):
[1329] Fix | Delete
"""Same as self.assertTrue(isinstance(obj, cls)), with a nicer
[1330] Fix | Delete
default message."""
[1331] Fix | Delete
if not isinstance(obj, cls):
[1332] Fix | Delete
standardMsg = '%s is not an instance of %r' % (safe_repr(obj), cls)
[1333] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1334] Fix | Delete
[1335] Fix | Delete
def assertNotIsInstance(self, obj, cls, msg=None):
[1336] Fix | Delete
"""Included for symmetry with assertIsInstance."""
[1337] Fix | Delete
if isinstance(obj, cls):
[1338] Fix | Delete
standardMsg = '%s is an instance of %r' % (safe_repr(obj), cls)
[1339] Fix | Delete
self.fail(self._formatMessage(msg, standardMsg))
[1340] Fix | Delete
[1341] Fix | Delete
def assertRaisesRegex(self, expected_exception, expected_regex,
[1342] Fix | Delete
*args, **kwargs):
[1343] Fix | Delete
"""Asserts that the message in a raised exception matches a regex.
[1344] Fix | Delete
[1345] Fix | Delete
Args:
[1346] Fix | Delete
expected_exception: Exception class expected to be raised.
[1347] Fix | Delete
expected_regex: Regex (re.Pattern object or string) expected
[1348] Fix | Delete
to be found in error message.
[1349] Fix | Delete
args: Function to be called and extra positional args.
[1350] Fix | Delete
kwargs: Extra kwargs.
[1351] Fix | Delete
msg: Optional message used in case of failure. Can only be used
[1352] Fix | Delete
when assertRaisesRegex is used as a context manager.
[1353] Fix | Delete
"""
[1354] Fix | Delete
context = _AssertRaisesContext(expected_exception, self, expected_regex)
[1355] Fix | Delete
return context.handle('assertRaisesRegex', args, kwargs)
[1356] Fix | Delete
[1357] Fix | Delete
def assertWarnsRegex(self, expected_warning, expected_regex,
[1358] Fix | Delete
*args, **kwargs):
[1359] Fix | Delete
"""Asserts that the message in a triggered warning matches a regexp.
[1360] Fix | Delete
Basic functioning is similar to assertWarns() with the addition
[1361] Fix | Delete
that only warnings whose messages also match the regular expression
[1362] Fix | Delete
are considered successful matches.
[1363] Fix | Delete
[1364] Fix | Delete
Args:
[1365] Fix | Delete
expected_warning: Warning class expected to be triggered.
[1366] Fix | Delete
expected_regex: Regex (re.Pattern object or string) expected
[1367] Fix | Delete
to be found in error message.
[1368] Fix | Delete
args: Function to be called and extra positional args.
[1369] Fix | Delete
kwargs: Extra kwargs.
[1370] Fix | Delete
msg: Optional message used in case of failure. Can only be used
[1371] Fix | Delete
when assertWarnsRegex is used as a context manager.
[1372] Fix | Delete
"""
[1373] Fix | Delete
context = _AssertWarnsContext(expected_warning, self, expected_regex)
[1374] Fix | Delete
return context.handle('assertWarnsRegex', args, kwargs)
[1375] Fix | Delete
[1376] Fix | Delete
def assertRegex(self, text, expected_regex, msg=None):
[1377] Fix | Delete
"""Fail the test unless the text matches the regular expression."""
[1378] Fix | Delete
if isinstance(expected_regex, (str, bytes)):
[1379] Fix | Delete
assert expected_regex, "expected_regex must not be empty."
[1380] Fix | Delete
expected_regex = re.compile(expected_regex)
[1381] Fix | Delete
if not expected_regex.search(text):
[1382] Fix | Delete
standardMsg = "Regex didn't match: %r not found in %r" % (
[1383] Fix | Delete
expected_regex.pattern, text)
[1384] Fix | Delete
# _formatMessage ensures the longMessage option is respected
[1385] Fix | Delete
msg = self._formatMessage(msg, standardMsg)
[1386] Fix | Delete
raise self.failureException(msg)
[1387] Fix | Delete
[1388] Fix | Delete
def assertNotRegex(self, text, unexpected_regex, msg=None):
[1389] Fix | Delete
"""Fail the test if the text matches the regular expression."""
[1390] Fix | Delete
if isinstance(unexpected_regex, (str, bytes)):
[1391] Fix | Delete
unexpected_regex = re.compile(unexpected_regex)
[1392] Fix | Delete
match = unexpected_regex.search(text)
[1393] Fix | Delete
if match:
[1394] Fix | Delete
standardMsg = 'Regex matched: %r matches %r in %r' % (
[1395] Fix | Delete
text[match.start() : match.end()],
[1396] Fix | Delete
unexpected_regex.pattern,
[1397] Fix | Delete
text)
[1398] Fix | Delete
# _formatMessage ensures the longMessage option is respected
[1399] Fix | Delete
msg = self._formatMessage(msg, standardMsg)
[1400] Fix | Delete
raise self.failureException(msg)
[1401] Fix | Delete
[1402] Fix | Delete
[1403] Fix | Delete
def _deprecate(original_func):
[1404] Fix | Delete
def deprecated_func(*args, **kwargs):
[1405] Fix | Delete
warnings.warn(
[1406] Fix | Delete
'Please use {0} instead.'.format(original_func.__name__),
[1407] Fix | Delete
DeprecationWarning, 2)
[1408] Fix | Delete
return original_func(*args, **kwargs)
[1409] Fix | Delete
return deprecated_func
[1410] Fix | Delete
[1411] Fix | Delete
# see #9424
[1412] Fix | Delete
failUnlessEqual = assertEquals = _deprecate(assertEqual)
[1413] Fix | Delete
failIfEqual = assertNotEquals = _deprecate(assertNotEqual)
[1414] Fix | Delete
failUnlessAlmostEqual = assertAlmostEquals = _deprecate(assertAlmostEqual)
[1415] Fix | Delete
failIfAlmostEqual = assertNotAlmostEquals = _deprecate(assertNotAlmostEqual)
[1416] Fix | Delete
failUnless = assert_ = _deprecate(assertTrue)
[1417] Fix | Delete
failUnlessRaises = _deprecate(assertRaises)
[1418] Fix | Delete
failIf = _deprecate(assertFalse)
[1419] Fix | Delete
assertRaisesRegexp = _deprecate(assertRaisesRegex)
[1420] Fix | Delete
assertRegexpMatches = _deprecate(assertRegex)
[1421] Fix | Delete
assertNotRegexpMatches = _deprecate(assertNotRegex)
[1422] Fix | Delete
[1423] Fix | Delete
[1424] Fix | Delete
[1425] Fix | Delete
class FunctionTestCase(TestCase):
[1426] Fix | Delete
"""A test case that wraps a test function.
[1427] Fix | Delete
[1428] Fix | Delete
This is useful for slipping pre-existing test functions into the
[1429] Fix | Delete
unittest framework. Optionally, set-up and tidy-up functions can be
[1430] Fix | Delete
supplied. As with TestCase, the tidy-up ('tearDown') function will
[1431] Fix | Delete
always be called if the set-up ('setUp') function ran successfully.
[1432] Fix | Delete
"""
[1433] Fix | Delete
[1434] Fix | Delete
def __init__(self, testFunc, setUp=None, tearDown=None, description=None):
[1435] Fix | Delete
super(FunctionTestCase, self).__init__()
[1436] Fix | Delete
self._setUpFunc = setUp
[1437] Fix | Delete
self._tearDownFunc = tearDown
[1438] Fix | Delete
self._testFunc = testFunc
[1439] Fix | Delete
self._description = description
[1440] Fix | Delete
[1441] Fix | Delete
def setUp(self):
[1442] Fix | Delete
if self._setUpFunc is not None:
[1443] Fix | Delete
self._setUpFunc()
[1444] Fix | Delete
[1445] Fix | Delete
def tearDown(self):
[1446] Fix | Delete
if self._tearDownFunc is not None:
[1447] Fix | Delete
self._tearDownFunc()
[1448] Fix | Delete
[1449] Fix | Delete
def runTest(self):
[1450] Fix | Delete
self._testFunc()
[1451] Fix | Delete
[1452] Fix | Delete
def id(self):
[1453] Fix | Delete
return self._testFunc.__name__
[1454] Fix | Delete
[1455] Fix | Delete
def __eq__(self, other):
[1456] Fix | Delete
if not isinstance(other, self.__class__):
[1457] Fix | Delete
return NotImplemented
[1458] Fix | Delete
[1459] Fix | Delete
return self._setUpFunc == other._setUpFunc and \
[1460] Fix | Delete
self._tearDownFunc == other._tearDownFunc and \
[1461] Fix | Delete
self._testFunc == other._testFunc and \
[1462] Fix | Delete
self._description == other._description
[1463] Fix | Delete
[1464] Fix | Delete
def __hash__(self):
[1465] Fix | Delete
return hash((type(self), self._setUpFunc, self._tearDownFunc,
[1466] Fix | Delete
self._testFunc, self._description))
[1467] Fix | Delete
[1468] Fix | Delete
def __str__(self):
[1469] Fix | Delete
return "%s (%s)" % (strclass(self.__class__),
[1470] Fix | Delete
self._testFunc.__name__)
[1471] Fix | Delete
[1472] Fix | Delete
def __repr__(self):
[1473] Fix | Delete
return "<%s tec=%s>" % (strclass(self.__class__),
[1474] Fix | Delete
self._testFunc)
[1475] Fix | Delete
[1476] Fix | Delete
def shortDescription(self):
[1477] Fix | Delete
if self._description is not None:
[1478] Fix | Delete
return self._description
[1479] Fix | Delete
doc = self._testFunc.__doc__
[1480] Fix | Delete
return doc and doc.split("\n")[0].strip() or None
[1481] Fix | Delete
[1482] Fix | Delete
[1483] Fix | Delete
class _SubTest(TestCase):
[1484] Fix | Delete
[1485] Fix | Delete
def __init__(self, test_case, message, params):
[1486] Fix | Delete
super().__init__()
[1487] Fix | Delete
self._message = message
[1488] Fix | Delete
self.test_case = test_case
[1489] Fix | Delete
self.params = params
[1490] Fix | Delete
self.failureException = test_case.failureException
[1491] Fix | Delete
[1492] Fix | Delete
def runTest(self):
[1493] Fix | Delete
raise NotImplementedError("subtests cannot be run directly")
[1494] Fix | Delete
[1495] Fix | Delete
def _subDescription(self):
[1496] Fix | Delete
parts = []
[1497] Fix | Delete
if self._message is not _subtest_msg_sentinel:
[1498] Fix | Delete
parts.append("[{}]".format(self._message))
[1499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function