Some assert functions are renamed/missing in various Python versions

Bug #1373139 reported by Adam Harwell
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
testtools
Fix Released
Undecided
Unassigned

Bug Description

Documentation for unittest in PY2/PY3:

https://docs.python.org/2/library/unittest.html#test-cases (see functions noted with "New in version 2.7.")
https://docs.python.org/3/library/unittest.html#test-cases (see functions noted with "New in version 3.x.")

For example, to do a fuzzy-comparison of two collections:

Python 2.7 unittest includes unittest.TestCase.assertItemsEqual( ).
Python 3.2+ unittest includes unittest.TestCase.assertCountEqual( ).
Python 2.6 unittest does not include either of these functions.

As far as I can tell, there's nothing in `six` to handle this. I've had to work around the problem in my own classes that implement testtools.TestCase, using the following:

if hasattr(testtools.TestCase, 'assertItemsEqual'):
    # If this function is available, do nothing (PY27)
    pass
elif hasattr(testtools.TestCase, 'assertCountEqual'):
    # If this function is available, alias it (PY32+)
    assertItemsEqual = testtools.TestCase.assertCountEqual
else:
    # If neither is available, make our own version (PY26, PY30-31?)
    def assertItemsEqual(self, expected_seq, actual_seq, msg=None):
        # Custom implementation is here

I believe this should be handled at the testtools layer.

Expected functionality for any version of Python (assuming the best option is to just make both names available):

self.assertItemsEqual(list1, list2) -> does comparison
self.assertCountEqual(list1, list2) -> does comparison

Actual functionality:

Py26: self.assertItemsEqual(list1, list2) -> exception raised, no method found
Py27: self.assertItemsEqual(list1, list2) -> does comparison
Py31: self.assertItemsEqual(list1, list2) -> exception raised, no method found
Py32+: self.assertItemsEqual(list1, list2) -> exception raised, no method found

Py26: self.assertCountEqual(list1, list2) -> exception raised, no method found
Py27: self.assertCountEqual(list1, list2) -> exception raised, no method found
Py31: self.assertCountEqual(list1, list2) -> exception raised, no method found
Py32+: self.assertCountEqual(list1, list2) -> does comparison

Revision history for this message
Adam Harwell (adam-harwell) wrote :

Most of the functions that are missing from unittest in Python 2.6.x are available via unittest2.

no longer affects: python-barbicanclient
description: updated
Revision history for this message
Robert Collins (lifeless) wrote :

Sorry, what is the bug?

Changed in testtools:
status: New → Incomplete
Revision history for this message
Adam Harwell (adam-harwell) wrote :

Inheriting from testtools.TestCase gives access to certain assert functions that are either missing or behave differently in different python versions, which makes writing polyglot code (like in OpenStack and many other projects) difficult. The only place I can think of that this fix would go is in testtools. See the original description for details on specifics. I'm also on Freenode as rm_you if you'd like me to run through the issue.

Revision history for this message
Adam Harwell (adam-harwell) wrote :

Ok, per our conversation on IRC, I have re-tested and it does look like this was resolved. Thanks!

Changed in testtools:
status: Incomplete → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.