I'm not sure you really do need to test this, but if you do…
In CPython 2.7, there's really no point in testing this. The elements will be in arbitrary order—which means they could arbitrarily end up in the same order as insertion order, and there's no way to force them not to be.
In CPython 3.3-3.5, it's even worse. The elements will not only be in arbitrary order, but in a different arbitrary order each time you run the tests. Which means your test may look like it's working because it happens to have a 3!-1 / 3!
chance of working, and then you'll check in a flaky and useless test.
In CPython 3.6-3.7, however, the order actually will be similar to OrderedDict
, but not quite identical, and that means there is something you can reliably test for. If you delete from a dict and then insert, the old slot will get reused. This is, of course, not true for OrderedDict
. So:
>>> d1, d2 = {}, collections.OrderedDict()
>>> for i in range(10):
... d1[i] = d2[i] = i
>>> del d1[2]
>>> del d2[2]
>>> d1[2] = d2[2] = 1000
>>> d1
{0: 0, 1: 1, 2: 1000, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9
>>> d2
OrderedDict([(0, 0),
(1, 1),
(3, 3),
(4, 4),
(5, 5),
(6, 6),
(7, 7),
(8, 8),
(9, 9),
(2, 1000)])
However, this is relying on implementation details of CPython 3.6-3.7, which is pretty much the thing you were trying to test that you weren't doing. Is it acceptable to do that in that test?