Asserts that two iterables have the same elements, the same number of times, without regard to order. self.assertEqual(Counter(list(first)), Counter(list(second))) Example: - [0, 1, 1] and [1, 0, 1] compare equal. -
(self, first, second, msg=None)
| 1224 | self.fail(self._formatMessage(msg, standardMsg)) |
| 1225 | |
| 1226 | def assertCountEqual(self, first, second, msg=None): |
| 1227 | """Asserts that two iterables have the same elements, the same number of |
| 1228 | times, without regard to order. |
| 1229 | |
| 1230 | self.assertEqual(Counter(list(first)), |
| 1231 | Counter(list(second))) |
| 1232 | |
| 1233 | Example: |
| 1234 | - [0, 1, 1] and [1, 0, 1] compare equal. |
| 1235 | - [0, 0, 1] and [0, 1] compare unequal. |
| 1236 | |
| 1237 | """ |
| 1238 | first_seq, second_seq = list(first), list(second) |
| 1239 | try: |
| 1240 | first = collections.Counter(first_seq) |
| 1241 | second = collections.Counter(second_seq) |
| 1242 | except TypeError: |
| 1243 | # Handle case with unhashable elements |
| 1244 | differences = _count_diff_all_purpose(first_seq, second_seq) |
| 1245 | else: |
| 1246 | if first == second: |
| 1247 | return |
| 1248 | differences = _count_diff_hashable(first_seq, second_seq) |
| 1249 | |
| 1250 | if differences: |
| 1251 | standardMsg = 'Element counts were not equal:\n' |
| 1252 | lines = ['First has %d, Second has %d: %r' % diff for diff in differences] |
| 1253 | diffMsg = '\n'.join(lines) |
| 1254 | standardMsg = self._truncateMessage(standardMsg, diffMsg) |
| 1255 | msg = self._formatMessage(msg, standardMsg) |
| 1256 | self.fail(msg) |
| 1257 | |
| 1258 | def assertMultiLineEqual(self, first, second, msg=None): |
| 1259 | """Assert that two multi-line strings are equal.""" |