[devtools/testlib] Port CubicWebTC.view() to py3k
authorRémi Cardona <remi.cardona@logilab.fr>
Mon, 14 Sep 2015 18:10:20 +0200
changeset 10621 987fbc88f097
parent 10620 30ebd6b2eaf3
child 10622 3cc6154b94a3
[devtools/testlib] Port CubicWebTC.view() to py3k testlib.TestCase.set_description's argument will eventually be printed out to sys.stdout/stderr. On python 2, this object takes bytes-like objects (eg, str) whereas python 3's takes unicode-like objects and handles the encoding on its own. Thus the 'str' type can used in both versions to eventually display the description on screen.
devtools/test/unittest_testlib.py
devtools/testlib.py
--- a/devtools/test/unittest_testlib.py	Mon Sep 14 18:12:44 2015 +0200
+++ b/devtools/test/unittest_testlib.py	Mon Sep 14 18:10:20 2015 +0200
@@ -17,10 +17,10 @@
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 """unittests for cw.devtools.testlib module"""
 
-from cStringIO import StringIO
-from io import BytesIO
+from io import BytesIO, StringIO
+from unittest import TextTestRunner
 
-from unittest import TextTestRunner
+from six import PY2
 
 from logilab.common.testlib import TestSuite, TestCase, unittest_main
 from logilab.common.registry import yes
@@ -52,7 +52,7 @@
 class WebTestTC(TestCase):
 
     def setUp(self):
-        output = StringIO()
+        output = BytesIO() if PY2 else StringIO()
         self.runner = TextTestRunner(stream=output)
 
     def test_error_raised(self):
--- a/devtools/testlib.py	Mon Sep 14 18:12:44 2015 +0200
+++ b/devtools/testlib.py	Mon Sep 14 18:10:20 2015 +0200
@@ -908,8 +908,11 @@
         view = viewsreg.select(vid, req, rset=rset, **kwargs)
         # set explicit test description
         if rset is not None:
+            # coerce to "bytes" on py2 because the description will be sent to
+            # sys.stdout/stderr which takes "bytes" on py2 and "unicode" on py3
+            rql = str(rset.printable_rql())
             self.set_description("testing vid=%s defined in %s with (%s)" % (
-                vid, view.__module__, rset.printable_rql()))
+                vid, view.__module__, rql))
         else:
             self.set_description("testing vid=%s defined in %s without rset" % (
                 vid, view.__module__))