diff --git a/_pytest/python.py b/_pytest/python.py index f214aef94..6189ef362 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1339,7 +1339,7 @@ class RaisesContext(object): # builtin pytest.approx helper -class approx: +class approx(object): """ assert that two numbers (or two sets of numbers) are equal to each other within some margin. @@ -1410,10 +1410,11 @@ class approx: def __repr__(self): from collections import Iterable - plus_minus = lambda x: '{} \u00B1 {:.1e}'.format(x, self._get_margin(x)) + utf_8 = lambda s: s.encode('utf-8') if sys.version_info.major == 2 else s + plus_minus = lambda x: utf_8(u'{} \u00b1 {:.1e}'.format(x, self._get_margin(x))) if isinstance(self.expected, Iterable): - return str([plus_minus(x) for x in self.expected]) + return ', '.join([plus_minus(x) for x in self.expected]) else: return plus_minus(self.expected) diff --git a/testing/python/approx.py b/testing/python/approx.py index 432ca4748..2f37c9b9b 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -27,6 +27,7 @@ class TestApprox: runner.run(test) def test_repr_string(self): + # Just make sure the Unicode handling doesn't raise any exceptions. print(pytest.approx(1.0)) - assert repr(pytest.approx(1.0)) == '1.0 ± 1.0e-06' + print(pytest.approx([1.0, 2.0, 3.0]))