Title: unittest.Testcase assertDictContainsSubset with integer keys
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, michael.foord, mnewman
Priority: normal Keywords:

Created on 2010-02-18 12:26 by mnewman, last changed 2010-02-18 21:57 by gregory.p.smith. This issue is now closed.

File name Uploaded Description Edit mnewman, 2010-02-18 12:26 assertDictContainsSubset test suite
Messages (3)
msg99500 - (view) Author: Michael Newman (mnewman) Date: 2010-02-18 12:26
The attached example unit test file shows that assertDictContainsSubset cannot handle error messages that need to show integer keys. Below is the output of the test suite, where "test_mixed_keys_fail" has an error (code mistake), while "test_text_keys_fail" produces a failure (result mistake) as expected.

ERROR: test_mixed_keys_fail (__main__.Test_one_and_one)
Traceback (most recent call last):
  File "", line 30, in test_mixed_keys_fail
    self.assertDictContainsSubset({3: "this does not exist"}, self.dict_with_mix
  File "C:\python31\lib\", line 908, in assertDictContainsSubset
    standardMsg = 'Missing: %r' % ','.join(missing)
TypeError: sequence item 0: expected str instance, int found

FAIL: test_text_keys_fail (__main__.Test_one_and_one)
Traceback (most recent call last):
  File "", line 33, in test_text_keys_fail
    self.assertDictContainsSubset({"3": "this does not exist"}, self.dict_with_t
AssertionError: Missing: '3'

Ran 4 tests in 0.010s

FAILED (failures=1, errors=1)
msg99526 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-02-18 20:20
Thanks for reporting this.

I can fix this particular error easily by repr'ing the keys. In the process I've found another fun way of killing this assert method:

        one = ''.join(chr(i) for i in range(255))
        two = u'\uFFFD'
        first = {'foo': one}
        second = {'foo': two}
        self.assertDictContainsSubset(first, second)

  File "/Users/michael/Dev/Packages/python-trunk/Lib/unittest/", line 728, in assertDictContainsSubset
    (key, value, actual[key]))
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 143: ordinal not in range(128)
msg99531 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-02-18 21:37
Fixed in revision 78229.
Date User Action Args
2010-02-18 21:57:43gregory.p.smithsetnosy: + gregory.p.smith
2010-02-18 21:37:54michael.foordsetstatus: open -> closed
resolution: fixed
messages: + msg99531

stage: resolved
2010-02-18 20:20:34michael.foordsetmessages: + msg99526
2010-02-18 14:33:53r.david.murraysetnosy: + michael.foord
2010-02-18 12:26:52mnewmancreate