From bf97d5b81735c6bdf85b27deeac2f83f94d436ad Mon Sep 17 00:00:00 2001 From: Kale Kundert Date: Mon, 7 Mar 2016 16:40:41 -0800 Subject: [PATCH] Use the plus/minus unicode symbol in the repr string. This was a challenge because it had to work in python2 and python3, which have almost opposite unicode models, and I couldn't use the six library. I'm also not sure the solution I found would work in python3 before python3.3, because I use the u'' string prefix which I think was initially not part of python3. --- _pytest/python.py | 7 ++++--- testing/python/approx.py | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) 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]))