New issue 462: What is the correct spelling when using unicode in assertions? https://bitbucket.org/hpk42/pytest/issue/462/what-is-the-correct-spelling-when-using
Charlie Clark: I'm really struggling with a way to do this that will work with Python 2 and 3. I have a simple assertion in a file encoded as UTF-8 ` assert ws['A16'].value == '=IF(ISBLANK(B16), "Düsseldorf", B16)'` Unfortunately, this leads to an unholy failure because pytest seems to be running some internal conversion: ``` #!python ___________________________________________________________ test_read_complex_formulae ____________________________________________________________ def test_read_complex_formulae(): null_file = os.path.join(DATADIR, 'reader', 'formulae.xlsx') wb = load_workbook(null_file) ws = wb.get_active_sheet() # Test normal forumlae assert ws.cell('A1').data_type != 'f' assert ws.cell('A2').data_type != 'f' assert ws.cell('A3').data_type == 'f' assert 'A3' not in ws.formula_attributes assert ws.cell('A3').value == '=12345' assert ws.cell('A4').data_type == 'f' assert 'A4' not in ws.formula_attributes assert ws.cell('A4').value == '=A2+A3' assert ws.cell('A5').data_type == 'f' assert 'A5' not in ws.formula_attributes assert ws.cell('A5').value == '=SUM(A2:A4)' # Test unicode > assert ws['A16'].value == '=IF(ISBLANK(B16), "Düsseldorf", B16)' null_file = '/Users/charlieclark/Projects/openpyxl/openpyxl/tests/test_data/reader/formulae.xlsx' wb = <openpyxl.workbook.Workbook object at 0x107773110> ws = <Worksheet "Sheet1"> openpyxl/tests/test_read.py:261: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ops = ('==',), results = (False,), expls = ('%(py3)s\n{%(py3)s = %(py1)s.value\n} == %(py6)s',) each_obj = ('=IF(ISBLANK(B16), "Düsseldorf", B16)', '=IF(ISBLANK(B16), "D\xc3\xbcsseldorf", B16)') def _call_reprcompare(ops, results, expls, each_obj): for i, res, expl in zip(range(len(ops)), results, expls): try: done = not res except Exception: done = True if done: break if util._reprcompare is not None: > custom = util._reprcompare(ops[i], each_obj[i], each_obj[i + 1]) done = True each_obj = ('=IF(ISBLANK(B16), "Düsseldorf", B16)', '=IF(ISBLANK(B16), "D\xc3\xbcsseldorf", B16)') expl = '%(py3)s\n{%(py3)s = %(py1)s.value\n} == %(py6)s' expls = ('%(py3)s\n{%(py3)s = %(py1)s.value\n} == %(py6)s',) i = 0 ops = ('==',) res = False results = (False,) lib/python2.7/site-packages/_pytest/assertion/rewrite.py:343: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ op = '==', left = '=IF(ISBLANK(B16), "Düsseldorf", B16)', right = '=IF(ISBLANK(B16), "D\xc3\xbcsseldorf", B16)' def callbinrepr(op, left, right): hook_result = item.ihook.pytest_assertrepr_compare( config=item.config, op=op, left=left, right=right) for new_expl in hook_result: if new_expl: # Don't include pageloads of data unless we are very # verbose (-vv) > if (len(py.builtin._totext('').join(new_expl[1:])) > 80*8 and item.config.option.verbose < 2): E UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 22: ordinal not in range(128) hook_result = [['\'=IF(ISBLANK(...eldorf", B16)\' == \'=IF(ISBLANK(B...eldorf", B16)\'', '- =IF(ISBLANK(B16), "Düsseldorf", B16)', '? ^', '+ =IF(ISBLANK(B16), "D\xc3\xbcsseldorf", B16)', '? ^^']] item = <Function 'test_read_complex_formulae'> left = '=IF(ISBLANK(B16), "Düsseldorf", B16)' new_expl = ['\'=IF(ISBLANK(...eldorf", B16)\' == \'=IF(ISBLANK(B...eldorf", B16)\'', '- =IF(ISBLANK(B16), "Düsseldorf", B16)', '? ^', '+ =IF(ISBLANK(B16), "D\xc3\xbcsseldorf", B16)', '? ^^'] op = '==' right = '=IF(ISBLANK(B16), "D\xc3\xbcsseldorf", B16)' lib/python2.7/site-packages/_pytest/assertion/__init__.py:83: UnicodeDecodeError ----------------------------------------------------------------- Captured stderr ----------------------------------------------------------------- /Users/charlieclark/Projects/openpyxl/openpyxl/tests/test_read.py:261: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal assert ws['A16'].value == '=IF(ISBLANK(B16), "Düsseldorf", B16)' /Users/charlieclark/Projects/openpyxl/lib/python2.7/site-packages/_pytest/assertion/util.py:178: UnicodeWarning: Unicode unequal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if left[i] != right[i]: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/difflib.py:439: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal a[besti+bestsize] == b[bestj+bestsize]: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/difflib.py:978: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if ai == bj: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/difflib.py:435: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal a[besti-1] == b[bestj-1]: ``` I've seen similar issues reported but nothing telling me the correct way to write the test. _______________________________________________ pytest-commit mailing list pytest-commit@python.org https://mail.python.org/mailman/listinfo/pytest-commit